Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
禁用javascript上的倒计时_Javascript_Setinterval_Countdown - Fatal编程技术网

禁用javascript上的倒计时

禁用javascript上的倒计时,javascript,setinterval,countdown,Javascript,Setinterval,Countdown,我有一个javascript代码,它可以在倒计时结束时给出一个错误。我需要创建一个代码来停止倒计时。它可以开始倒计时,但我不能阻止它。 这是我的密码: function countdown( elementName, minutes, seconds ) { var element, endTime, hours, mins, msLeft, time; function twoDigits( n ) { return (n <= 9 ? "0"

我有一个javascript代码,它可以在倒计时结束时给出一个错误。我需要创建一个代码来停止倒计时。它可以开始倒计时,但我不能阻止它。 这是我的密码:

function countdown( elementName, minutes, seconds )
{
    var element, endTime, hours, mins, msLeft, time;

    function twoDigits( n )
    {
        return (n <= 9 ? "0" + n : n);
    }

    function updateTimer()
    {
        msLeft = endTime - (+new Date);
        if ( msLeft < 1000 ) {
            givestrongerror(error_timeout);
        } else {
            time = new Date( msLeft );
            hours = time.getUTCHours();
            mins = time.getUTCMinutes();
            element.innerHTML = (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() );
            setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );
        }
    }

    element = document.getElementById( elementName );
    endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
    updateTimer();
}

一个选择是简单地清除超时
setTimeout()
返回一个超时id,可以使用
clearTimeout()
清除该id

在倒计时函数的底部,如果您想让用户停止,可以使用
stopCountdown
函数返回一个对象

return {
  stop: stopCountdown
}   

通过将
setTimeout
分配给变量,在
setTimeout
上使用
cleartimout

var countdown_timeout_id;

function startCountdown()
{
  countdown_timeout_id = window.setTimeout(function() { /*blah*/ }, 100);
}

function stopCountdown()
{
  window.clearTimeout(countdown_timeout_id);
}
//make timeout global
var timeout = null;
function countdown( elementName, minutes, seconds )
{
var element, endTime, hours, mins, msLeft, time;

function twoDigits( n )
{
    return (n <= 9 ? "0" + n : n);
}

function updateTimer()
{
    msLeft = endTime - (+new Date);
    if ( msLeft < 1000 ) {
        givestrongerror(error_timeout);
    } else {
        time = new Date( msLeft );
        hours = time.getUTCHours();
        mins = time.getUTCMinutes();
        element.innerHTML = (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() );

        //set variable 'timeout' to the setTimeout that could be erroneous
        timeout = setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );

    }
}

element = document.getElementById( elementName );
endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
updateTimer();
}

//call this function to end the timeout
function endTimeout(){
    clearTimeout(timeout);
}
//使超时为全局超时
var超时=null;
函数倒计时(元素名称、分钟、秒)
{
变量元素,结束时间,小时,分钟,msLeft,时间;
函数两位数(n)
{

return(n)您需要在它到达0之前停止它吗?因为它现在存在,当它到达0时它会停止。您能澄清错误是什么吗?似乎第一,您有一个错误,第二,当您想要停止倒计时时,您无法停止倒计时
//make timeout global
var timeout = null;
function countdown( elementName, minutes, seconds )
{
var element, endTime, hours, mins, msLeft, time;

function twoDigits( n )
{
    return (n <= 9 ? "0" + n : n);
}

function updateTimer()
{
    msLeft = endTime - (+new Date);
    if ( msLeft < 1000 ) {
        givestrongerror(error_timeout);
    } else {
        time = new Date( msLeft );
        hours = time.getUTCHours();
        mins = time.getUTCMinutes();
        element.innerHTML = (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() );

        //set variable 'timeout' to the setTimeout that could be erroneous
        timeout = setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );

    }
}

element = document.getElementById( elementName );
endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
updateTimer();
}

//call this function to end the timeout
function endTimeout(){
    clearTimeout(timeout);
}