1

我有一个时钟,我想一直滴答作响,除非我将“break”放入“seconds”参数中。当我确实输入“休息”时,时钟会中断一秒钟,然后继续滴答作响。我想让它停止滴答作响,我不知道问题是什么。作为记录,我在页面加载时调用此函数,然后每当用户单击页面上的元素时。他们点击某些东西后会变得很糟糕。

var seconds;

function countdown_clock(seconds) {
    if (seconds != "break") {
        countdown(seconds);

        seconds = seconds;
    }
}

function displaymessage() {
    alert("Your Time Has Expired");
}

function countdown(seconds) {
    if (seconds != "break") {
        Time_Left = seconds;
        format = 1;

        if (Time_Left < 0)
            Time_Left = 0;

        switch (format) {
            case 0:
                //The simplest way to display the time left.
                $("div#ClockCountdown").html(Time_Left + ' seconds');
                break;
            case 1:
                //More detailed.
                Next_time = Time_Left - 1;
                minutes = Math.floor(Time_Left / 60);
                Time_Left %= 60;
                seconds = Time_Left;

                mps = 's';
                sps = 's';
                earlyzero = '';
                latezero = '';
                //ps is short for plural suffix.

                if (minutes == 1) mps = '';
                if (seconds == 1) sps = '';
                if (seconds < 10) earlyzero = '0';
                //                    if(seconds == 10) latezero ='0';

                //                    document.all.countdown.innerHTML = minutes + ' minute' + mps + ' and ';
                //                    document.all.countdown.innerHTML += seconds + ' second' + sps;

                innerHTML = minutes + ':';
                innerHTML += earlyzero + seconds;
                $("div#ClockCountdown").html(innerHTML + "nonbroken!");
                break;
            default:
                $("div#ClockCountdown").html(Time_Left + ' seconds');
        }

        if (Next_time == 0) {
            displaymessage();
            window.location.href = "navigation.php?nav=video_timeout";
        }
        else {
            setTimeout('countdown(' + Next_time + ');', 1000);
        }
    }
}
4

2 回答 2

1

您还必须清除超时。做这样的事情:

var timeout;
function countdown(seconds) {
    if (seconds == "break") {
        clearTimeout(timeout);
    }
    // other things
    if( Next_time == 0) {
        displaymessage();
        window.location.href ="navigation.php?nav=video_timeout";
    } else {
        timeout = setTimeout('countdown(' + Next_time + ');', 1000);
    }
}
于 2012-07-20T19:47:19.200 回答
0

超时是一击,当调用超时函数时它们被清除。仅当秒 !== 'break' 时才应设置下一个超时:

    if (Next_time == 0) {
        displaymessage();
        window.location.href = "navigation.php?nav=video_timeout";
    }
    else if (seconds !== 'break') {
        setTimeout('countdown(' + Next_time + ');', 1000);
    }
于 2012-07-20T20:35:53.520 回答