简体   繁体   English

如何在asp.net Web应用程序(C#)中制作倒数计时器?

[英]How to make a countdown timer in asp.net web application(C#)?

I am creating timed online test in C# asp.net. 我正在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. 例如,用户正在回答问题10分钟并且意外地系统被绞死。 After recovery the countdown must start from 10 mins. 恢复后,倒计时必须从10分钟开始。 Can anyone please help me with coding in C#? 有谁能帮我用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");
}

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. 我认为最好在表中填写一个字段并在其中设置问题的总时间,并在每15/30秒(根据您所需的频率)更新该字段,并在每次更新时从总时间中减去15/30秒的值。 and do this till the field has value > 0. 并执行此操作直到该字段的值> 0。

for this you can take help of timer (javascript timer) and ajax. 为此你可以得到计时器(javascript计时器)和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. 使用ajax我认为最好创建一个webservice(它将更新该字段的值)并在javascript计时器的基础上使用ajax调用它。 well you can also use ajax timer. 那么你也可以使用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

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传递数据库数据,当数据库导致断开连接时,您将一些数据传递给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>

您可以使用Windows注册表存储计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM