Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.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 停止setInterval()函数一段时间_Javascript_Jquery - Fatal编程技术网

Javascript 停止setInterval()函数一段时间

Javascript 停止setInterval()函数一段时间,javascript,jquery,Javascript,Jquery,我一直在想如何临时停止setInterval()函数上的计时器。我使用它来旋转横幅,我希望它“重置计时器”,或者如果单击缩略图,它在特定图像上停留的时间更长 我已经有了单击设置的事件处理程序 这就是我到目前为止所做的: (注:为简化起见,其他代码除外) 任何建议/帮助都会很好!:) 这个怎么样 //Set interval of fade effect var timer = setInterval(bannerRotate, 7000 ); //Click function for bann

我一直在想如何临时停止setInterval()函数上的计时器。我使用它来旋转横幅,我希望它“重置计时器”,或者如果单击缩略图,它在特定图像上停留的时间更长

我已经有了单击设置的事件处理程序

这就是我到目前为止所做的:

(注:为简化起见,其他代码除外)

任何建议/帮助都会很好!:)

这个怎么样

//Set interval of fade effect
var timer = setInterval(bannerRotate, 7000 );

//Click function for banner thumbnails
$('#banner ul li .banner_thumb').click(function(){

    $('#banner ul li .banner_thumb').parent().removeClass("selected");
    $(this).parent().addClass("selected");

    // if you already have a timer running, kill it
    if (timer) {
        clearTimeout(timer);
    }

    // now re-bind the setTimeout to timer with a new delay
    timer = setTimeout(bannerRotate, 10000);
});

现在,如果您想“暂停”计时器,这可能更像您想要的:

var delayFor = 0, normalDelay = 7000, timer;
function bannerRotate() {
    if (delayFor) {
        if (timer) {
            clearTimeout(timer);
        }

        timer = setTimeout(function () {
            delayFor = 0;
            timer = setInterval(bannerRotate, normalDelay);
        }, delayFor);
    }

    // normal bannerRotate code here
}

//Set interval of fade effect
timer = setInterval(bannerRotate, normalDelay);

//Click function for banner thumbnails
$('#banner ul li .banner_thumb').click(function(){
    $('#banner ul li .banner_thumb').parent().removeClass("selected");
    $(this).parent().addClass("selected");

    // extend the current delay time by 3 seconds
    delayFor = 3000;
});
这个怎么样

//Set interval of fade effect
var timer = setInterval(bannerRotate, 7000 );

//Click function for banner thumbnails
$('#banner ul li .banner_thumb').click(function(){

    $('#banner ul li .banner_thumb').parent().removeClass("selected");
    $(this).parent().addClass("selected");

    // if you already have a timer running, kill it
    if (timer) {
        clearTimeout(timer);
    }

    // now re-bind the setTimeout to timer with a new delay
    timer = setTimeout(bannerRotate, 10000);
});

现在,如果您想“暂停”计时器,这可能更像您想要的:

var delayFor = 0, normalDelay = 7000, timer;
function bannerRotate() {
    if (delayFor) {
        if (timer) {
            clearTimeout(timer);
        }

        timer = setTimeout(function () {
            delayFor = 0;
            timer = setInterval(bannerRotate, normalDelay);
        }, delayFor);
    }

    // normal bannerRotate code here
}

//Set interval of fade effect
timer = setInterval(bannerRotate, normalDelay);

//Click function for banner thumbnails
$('#banner ul li .banner_thumb').click(function(){
    $('#banner ul li .banner_thumb').parent().removeClass("selected");
    $(this).parent().addClass("selected");

    // extend the current delay time by 3 seconds
    delayFor = 3000;
});

一个简单的可能性是设置一个标志,让bannerUpdate函数在决定是否真正更新之前查看它。

一个简单的可能性是设置一个标志,让bannerUpdate函数在决定是否真正更新之前查看它。

考虑使用
设置超时
,而不是
setInterval
并在
bannerRotate
功能结束时续订:

var timer = setTimeout("bannerRotate()", 7000);

$('#banner ul li .banner_thumb').click(function () {

     $('#banner ul li .banner_thumb').parent().removeClass("selected");
     $(this).parent().addClass("selected");
     //this is where I tried to make it wait but obviously this didn't work 
     clearTimeout(timer);
     setTimeout("bannerRotate()", 10000);

});

function bannerRotate() {
     //..your code
     timer = setTimeout("bannerRotate()", 7000);
}

考虑使用
setTimeout
而不是
setInterval
,并在
bannerRotate
功能结束时续订:

var timer = setTimeout("bannerRotate()", 7000);

$('#banner ul li .banner_thumb').click(function () {

     $('#banner ul li .banner_thumb').parent().removeClass("selected");
     $(this).parent().addClass("selected");
     //this is where I tried to make it wait but obviously this didn't work 
     clearTimeout(timer);
     setTimeout("bannerRotate()", 10000);

});

function bannerRotate() {
     //..your code
     timer = setTimeout("bannerRotate()", 7000);
}

setInterval
setTimeout
使用非
eval
重载-例如
setInterval(bannerRotate,7000)
。顺便说一句,将yor函数直接传递给setInterval,而不是使用字符串和eval:
setInterval(bannerRotate,10000)
setInterval
setTimeout
使用非
eval
重载-例如
setInterval(bannerRotate,7000)
。顺便说一句,将yor函数直接传递给setInterval,而不是使用字符串和eval:
setInterval(bannerRotate,10000)
MDN声明
window.clearInterval
不是任何标准的一部分——使用
setInterval
clearTimeout
是标准操作,可移植到非Mozzila实现?MDN声明
window.clearInterval
不是任何标准的一部分——不会最好使用
setInterval
clearTimeout
,这是标准操作,可移植到非Mozzila实现中?我同意Yuriy的观点,您只能在上一次旋转结束后旋转横幅,因此setTimeout在这里比setIntervalIf好得多,如果我可以检查两个答案的话。我也要检查一下这个!:D谢谢!我同意Yuriy的观点,你必须在上一次轮换结束后才轮换横幅,所以setTimeout在这里比setIntervali好得多如果我可以检查两个答案的话。我也要检查一下这个!:D谢谢!