asp.net vb.net 和 javascript 中的倒数计时器

Countdown timer in asp.net vb.net with javascript

美好的一天, 我看到这个 post 关于如何使用 javascript 在 asp.net c# 中实现倒数计时器。这是 link 到 post 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 分钟间隔后变为负值。 这是我的代码:

    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);
}

我想你需要这段代码。

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);*
        }

    }