如何在asp.net web应用程序(C#)中制作倒计时器?
我正在 C# asp.net 中创建定时在线测试。我是使用本地机器时间创建的。但在数据库断开连接或系统挂起的情况下,倒计时器必须从机器挂起或数据库意外断开的时间开始。例如,用户正在回答问题 10 分钟,系统意外挂起。恢复后,倒计时必须从 10 分钟开始。谁能帮我用 C# 编码吗?
我使用了以下代码,但它在上述场景中没有用。
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");
}
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#?
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的意见是使用 javascript 进行倒计时(也嵌入)并使用 json 传递数据库数据,当数据库导致断开连接时,您将一些数据传递给 javascript 并使用控制进行倒计时。
下载以下 javascript 文件并将其放入您的 Web 应用程序目录中:
http://scripts.hashemian.com/js/countdown.js
而不是使用一些东西像这样:
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.
Download the following javascript file and put into your web application directory:
http://scripts.hashemian.com/js/countdown.js
And than use something like this:
我认为最好在表中获取一个字段并设置其中问题的总时间,并每 15/30 秒更新该字段(根据您所需的频率),并且每次更新时从总时间中减去 15/30 秒的值。并执行此操作,直到该字段的值 > 0.
为此,您可以借助计时器(javascript计时器)和ajax。向服务器发送请求以更新该字段。
使用ajax,我认为最好创建一个web服务(它将更新该字段的值)并基于javascript计时器使用ajax调用它。好吧,你也可以使用ajax计时器。
javascript计时器参考:
http://dotnetacademy.blogspot.com/2010/09 /timer-in-javascript.html
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.
for this you can take help of timer (javascript timer) and ajax. to send request to server to update that field.
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.
javascript timer reference:
http://dotnetacademy.blogspot.com/2010/09/timer-in-javascript.html
您可以使用 Windows 寄存器来存储计数器。
You can use windows registery to store the counter.