5

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

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

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

2 回答 2

10

您可以保留一个run-count,并normal_duration + 30000用作X+ 1st time的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);
于 2013-06-04T16:47:26.830 回答
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();
于 2013-06-04T20:29:43.703 回答