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

发布于 2024-10-02 05:15:06 字数 1045 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦途 2024-10-09 05:15:06

我的意见是使用 javascript 进行倒计时(也嵌入)并使用 json 传递数据库数据,当数据库导致断开连接时,您将一些数据传递给 javascript 并使用控制进行倒计时。

下载以下 javascript 文件并将其放入您的 Web 应用程序目录中:
http://scripts.hashemian.com/js/countdown.js

而不是使用一些东西像这样:

<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>

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:

<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>
心不设防 2024-10-09 05:15:06

我认为最好在表中获取一个字段并设置其中问题的总时间,并每 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

-小熊_ 2024-10-09 05:15:06

您可以使用 Windows 寄存器来存储计数器。

You can use windows registery to store the counter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文