2017-03-20 81 views
0

美好的一天, 請我看到這篇文章如何實現在asp.net c#javascript中的倒計時器。這是鏈接到帖子https://forums.asp.net/t/1291427.aspx?how+to+create+a+countdown+timer+using+C+and+ASP+NET+AJAX。我還沒有完全修改它以適合我自己的程序;我的程序是在註冊付款後立即給用戶30分鐘,但我被卡住了。我想在0天00:00:00停止倒數計時器,並且從服務器端或客戶端執行數據庫操作,計時器在30分鐘間隔後變爲負數。 這是我的代碼有:倒計時器在asp.net vb.net與javascript

var leave = <%=seconds%> 
    CounterTimer(); 
    var interv=setInterval(CounterTimer,1000); 
    function CounterTimer() 
    { 
     var day = Math.floor(leave/(60 * 60 * 24)) 
     var hour = Math.floor(leave/3600) - (day * 24) 
     var minute = Math.floor(leave/60) - (day * 24 *60) - (hour * 60) 
     var second = Math.floor(leave) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60) 


     hour=hour<10 ? "0" + hour : hour; 
     minute=minute<10 ? "0" + minute : minute; 
     second=second<10 ? "0" + second : second; 


     var remain=day + " days "+hour + ":"+minute+":"+second; 
     leave=leave-1; 


     document.getElementById("timelabel").innerText=remain; 
     if (minute == 0 & second == 0) { 
      return; 
     } 

    } 

public double seconds; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    seconds = (GetEndTime() - GetStartTime()).TotalSeconds; 


} 
private DateTime GetStartTime() 
{ 
    return DateTime.Now; 
} 
private DateTime GetEndTime() 
{ 
    return new DateTime(2008, 7, 23, 8, 10, 0); 
} 

回答

0

我想你想這個代碼。

function CounterTimer() { 
     var day = Math.floor(leave/(60 * 60 * 24)) 
     var hour = Math.floor(leave/3600) - (day * 24) 
     var minute = Math.floor(leave/60) - (day * 24 * 60) - (hour * 60) 
     var second = Math.floor(leave) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60) 


     hour = hour < 10 ? "0" + hour : hour; 
     minute = minute < 10 ? "0" + minute : minute; 
     second = second < 10 ? "0" + second : second; 


     var remain = day + " days " + hour + ":" + minute + ":" + second; 
     leave = leave - 1; 


     document.getElementById("timelabel").innerText = remain; 
     if (minute == 0 & second == 0) { 
      (i think better safety minute. <= 0 & second <= 0) 
      *clearInterval(interv);* 
     } 

    }