0

您好,我有一个像这样通过 php/html 执行的计时器

// timer
print "<script>countdown();</script>";

但是我有一个看起来像这样的 jquery 警告对话框,我希望 javascript 函数在他们单击关闭按钮后重新启动

    // Create timeout warning dialog
    $('body').append('<div title="Timeout"</div>');
    $('#sessionTimeout-dialog').dialog({
        autoOpen: false,
        width: 600,
        modal: true,
        closeOnEscape: false,
        open: function(event, ui) { $(".dialog").hide(); },
        buttons: {
            // Button two - closes dialog and makes call to keep-alive URL
            "Continue Session": function() {
                $(this).dialog('close');
                clearTime();
                $.ajax({
                    type: 'POST',
                    url: o.keepAliveUrl
                });


                countdown(); //I call the javascript function in hopes of making it go again
            }
        }
    });

但是,一旦单击按钮继续,我似乎无法让计时器真正重新启动。这是我的计时器代码,我做错了什么?我在按钮关闭后调用 javascript 函数,但没有任何反应,它只是变成负数

      // set minutes
var mins = 2;

// calculate the seconds (don't change this! unless time progresses at a different speed for you...)
var secs = mins * 60;   

function countdown() {
    setTimeout('Decrement()',1000);
}

function Decrement() {
    if (document.getElementById) {
        minutes = document.getElementById("minutes");
        seconds = document.getElementById("seconds");
    // if less than a minute remaining
    if (seconds < 59) {
        seconds.value = secs;
    } else {
        minutes.value = getminutes();
        seconds.value = getseconds();
    }
        secs--;
        setTimeout('Decrement()',1000);
    }
}

function getminutes() {
    // minutes is seconds divided by 60, rounded down
    mins = Math.floor(secs / 60);
    return mins;
}

function getseconds() {
    // take mins remaining (as seconds) away from total seconds remaining
    return secs-Math.round(mins *60);
}
4

2 回答 2

0

看起来您的倒计时功能不会重置计时器,而只会启动递减循环。

添加类似的东西

function resetCountdown()
{
    secs = mins * 2;
}

并在对话框关闭时调用此函数以重置倒计时。不要再次调用 countdown() ,因为这会在已经运行的计时器之外创建另一个计时器。

您的代码中也存在错误:

if (seconds < 59) {
    seconds.value = secs;
} else {
    minutes.value = getminutes();
    seconds.value = getseconds();
}

应该

if (secs < 59) {
    minutes.value = 0;
    seconds.value = secs;
} else {
    minutes.value = getminutes();
    seconds.value = getseconds();
}
于 2013-07-08T21:55:39.143 回答
0

我不完全确定这是您想要的,但是:

当窗口重新获得焦点时(从对话框中),我喜欢重新启动某些计时器。下面是一个例子:

<html>
    <head>
        <script>
            g = {};
            g.timer = false;
            g.count = false;
            g.waittime = 3; //seconds for timer.
            g.timeleft = (g.waittime * 100) - 10;
            checktimer = function(){
                if(g.timer === false){
                    g.timer = setTimeout(function(){
                        g.timer = false;
                        g.timeleft = g.waittime * 100;
                        alert("Hi!");
                    },g.waittime * 1000);
                }
                if(g.count === false){
                    g.count = setInterval(function(){
                        g.timeleft-=10;
                        document.getElementById("disp").innerText =(g.timeleft/100).toString().match(/^\d+(?:\.\d{0,2})?/);
                    },100);
                }
            }
            window.onfocus = function() {
                checktimer();
            };

        </script>
    </head>
    <body onload="checktimer();">
        <div>Time Left: <span id="disp">0</span> Seconds.</div>
    </body>
</html>
于 2013-07-08T22:39:54.280 回答