多次运行setInterval,然后暂停几秒钟

时间:2013-06-04 16:44:07

标签: javascript jquery settimeout setinterval

我需要我的代码运行x次,然后在恢复之前暂停30秒左右。有什么想法吗?

myslidefunction();
var tid = setInterval(myslidefunction, 1000);

function myslidefunction() {
    setTimeout(function () {
        //do stuff  
    }, 400); 
};

2 个答案:

答案 0 :(得分:10)

您可以保留运行计数,并使用normal_duration + 30000作为 X + 1st 时间的setTimeout延迟。

var runCount = 0, runsBeforeDelay = 20;
function myslidefunction(){

    // .. stuff

    runCount++;
    var delay = 0;
    if(runCount > runsBeforeDelay) {
        runCount = 0;
        delay = 30000;
    }
    setTimeout(myslidefunction, 400 + delay);
};

// start it off
setTimeout(myslidefunction, 1000);

答案 1 :(得分:1)

var counter = 0;

var mySlideFunction = function(){

    /* your "do stuff" code here */

    counter++;
    if(counter>=10){
        counter = 0;
        setTimeout(mySlideFunction, 30000);
    }else{
        setTimeout(mySlideFunction, 1000);
    }
}

mySlideFunction();