[英]How to make a countdown timer in asp.net web application(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");
}
我認為最好在表中填寫一個字段並在其中設置問題的總時間,並在每15/30秒(根據您所需的頻率)更新該字段,並在每次更新時從總時間中減去15/30秒的值。 並執行此操作直到該字段的值> 0。
為此你可以得到計時器(javascript計時器)和ajax的幫助。 向服務器發送請求以更新該字段。
使用ajax我認為最好創建一個webservice(它將更新該字段的值)並在javascript計時器的基礎上使用ajax調用它。 那么你也可以使用ajax計時器。
javascript計時器參考:
http://dotnetacademy.blogspot.com/2010/09/timer-in-javascript.html
我的意見是使用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>
您可以使用Windows注冊表存儲計數器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.