在asp.net倒计时 [英] Countdown timer in asp.net
问题描述
我使用asp.net 3.5使用C#。
我想创建一个倒数计时器,我的要求是这样的:
I am using asp.net 3.5 with C#. I want to create a countdown timer and my requirement is like this:
倒计时结束日期:2010年6月16日
所以,直到6月16日来我的计时器显示的时间remeaning
Countdown end date: June 16 2010 So, till June 16 comes my timer will show the remeaning time.
请让我知道如何去实现它,我谷歌,但我没有得到excat解决我的问题。
Please let me know how to achieve it, I google it but i didn't get the excat solution to my problem.
先谢谢了。
推荐答案
这是你需要解决的东西的JavaScript。你需要从服务器唯一要做的是设定结束日期JavaScript变数。你需要使用Javascript,因为你只能从服务器加载页面。事后你需要处理客户端上的倒计时。
This is something you need to solve with Javascript. The only thing you need to do from the server is set the end date as a Javascript variable. You need Javascript because you only load the page from the server. Afterwards you need to handle the countdown on the client.
的JavaScript
<script type="text/javascript">
function countdown_clock(clockID, year, month, day, hour, minute) {
countdown(clockID, year, month, day, hour, minute);
}
function countdown(clockID, year, month, day, hour, minute) {
Today = new Date();
Todays_Year = Today.getFullYear();
Todays_Month = Today.getMonth();
//Convert both today's date and the target date into miliseconds.
Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();
//Find their difference, and convert that into seconds.
Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
if (Time_Left < 0)
Time_Left = 0;
days = Math.floor(Time_Left / (60 * 60 * 24));
Time_Left %= (60 * 60 * 24);
hours = Math.floor(Time_Left / (60 * 60));
Time_Left %= (60 * 60);
minutes = Math.floor(Time_Left / 60);
Time_Left %= 60;
seconds = Time_Left;
dps = 's'; hps = 's'; mps = 's'; sps = 's';
//ps is short for plural suffix.
if (days == 1) dps = '';
if (hours == 1) hps = '';
if (minutes == 1) mps = '';
if (seconds == 1) sps = '';
var clock = document.getElementById(clockID);
clock.innerHTML = days + ' day' + dps + ' ';
clock.innerHTML += hours + ' hour' + hps + ' ';
clock.innerHTML += minutes + ' minute' + mps + ' and ';
clock.innerHTML += seconds + ' second' + sps;
//Recursive call, keeps the clock ticking.
setTimeout('countdown("' + clockID + '",' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ');', 1000);
}
</script>
ASP.NET
protected override void OnPreRender(EventArgs e)
{
DateTime endDate = new DateTime(2010, 6, 1, 0, 0, 0);
string script = string.Format("countdown_clock('clock', {0}, {1}, {2}, {3}, {4});", endDate.Year, endDate.Month, endDate.Day, endDate.Hour, endDate.Minute);
ScriptManager.RegisterStartupScript(this, this.GetType(), "countdown", script, true);
base.OnPreRender(e);
}
剧本拍摄的修改从的My小脚本的。
这篇关于在asp.net倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!