javascript 如何在 asp.net web 应用程序(C#)中制作倒数计时器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4151532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a countdown timer in asp.net web application(C#)?
提问by Prem
I am creating timed online test in C# asp.net. I have created it using the local machine time. But in the case of Database disconnection or system hang, The countdown timer must start from the time at which the machine has hanged or unexpected database disconnectivity. For example the user is answering the Questions for 10 mins and unexpectedly system hanged. After recovery the countdown must start from 10 mins. Can anyone please help me with coding in C#?
我正在 C# asp.net 中创建定时在线测试。我使用本地机器时间创建了它。但是在数据库断开或系统挂起的情况下,倒计时必须从机器挂起或数据库意外断开的时间开始。例如,用户在回答问题 10 分钟后系统意外挂起。恢复后倒计时必须从 10 分钟开始。任何人都可以帮我用 C# 编码吗?
I have used the following code, But its not useful in the above scenario.
我使用了以下代码,但在上述场景中没有用。
int totalTime = (Convert.ToInt32(ViewState["totaltime"])) * 60;
DateTime startTime = (DateTime)ViewState["startTime"];
TimeSpan elaspedTime = DateTime.Now.Subtract(startTime);
Literal1.Text = elaspedTime.Hours.ToString() + "h" + ":" + elaspedTime.Minutes.ToString() +
"m" + ":" + elaspedTime.Seconds.ToString() + "s";
int finish = Convert.ToInt32(elaspedTime.TotalSeconds);
int remaingsec = (totalTime - finish);
TimeSpan remainingtime = TimeSpan.FromSeconds(remaingsec);
string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s",
remainingtime.Hours,
remainingtime.Minutes,
remainingtime.Seconds,
remainingtime.Milliseconds);
LIteral2.Text = answer;
if (totalTime == finish)
{
lnkFinish_Click(sender, e);
Response.Redirect(@"~/Error.aspx");
}
采纳答案by Andrea Turri
My opinion is to use javascript for the countdown (embedded as well) and to pass your DB data with json, when DB cause disconnection you pass some data to javascript and with a control you proced to the countdown.
我的意见是使用 javascript 进行倒计时(也嵌入)并使用 json 传递您的数据库数据,当 DB 导致断开连接时,您将一些数据传递给 javascript 并使用您进行倒计时的控件。
Download the following javascript file and put into your web application directory: http://scripts.hashemian.com/js/countdown.js
下载以下 javascript 文件并将其放入您的 Web 应用程序目录:http: //scripts.hashemian.com/js/countdown.js
And than use something like this:
而不是使用这样的东西:
<script type="text/javascript">
TargetDate = "12/25/207 12:01 AM";
BackColor = "red";
ForeColor = "white";
CountActive = true;
CountStepper = -1;
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is here!";
</script>
回答by Dr. Rajesh Rolen
i think its better to take a field in table and set total time for question in it and update that field on every 15/30 seconds (as per your required frequency) and on every update substract the value 15/30 second from total time. and do this till the field has value > 0.
我认为最好在表中获取一个字段并在其中设置问题的总时间并每 15/30 秒更新该字段(根据您所需的频率),并且每次更新时从总时间中减去 15/30 秒的值。并执行此操作,直到该字段的值 > 0。
for this you can take help of timer (javascript timer) and ajax. to send request to server to update that field.
为此,您可以借助计时器(javascript 计时器)和 ajax。向服务器发送请求以更新该字段。
with ajax i think its better to create a webservice (which will update value of that field) and call it using ajax on basis of javascript timer. well you can also use ajax timer.
使用ajax,我认为最好创建一个webservice(它将更新该字段的值)并根据javascript计时器使用ajax调用它。好吧,您也可以使用 ajax 计时器。
javascript timer reference:
javascript 计时器参考:
http://dotnetacademy.blogspot.com/2010/09/timer-in-javascript.html
http://dotnetacademy.blogspot.com/2010/09/timer-in-javascript.html
回答by Naresh Goradara
You can use windows registery to store the counter.
您可以使用 windows registery 来存储计数器。

