最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript setTimeout function - Stack Overflow

programmeradmin8浏览0评论

I am calling this function

function drawLayers() {

    //setTimeout(drawBlueSky,500);
    //setTimeout(drawCircle1,1250);
    setTimeout(drawMoon,800);
    setTimeout(drawCircle1,2300);
    setTimeout(drawCircle2,2700);
    setTimeout(drawCircle3,3100);
    setTimeout(drawCircle4,3500);
    setTimeout(drawCircle5,3900);
    setTimeout(drawtext2,4300);
    setTimeout(drawtext,4700);
    setTimeout(drawtext3,5100);
    setTimeout(drawtext4,5500);
    setTimeout(drawtext5,5900);
    setTimeout(drawtext6,6300);
    setTimeout(drawtext7,6700);
    setTimeout(drawtext8,7100);
    setTimeout(drawtext9,7500);
    setTimeout(drawtext10,7900);

}

which calls many other functions fox ex drawMoon,drawCircle1 etc I am calling drawLayers() function on click of play button. What I need is if someone clicks on stop button the setTimeout function should stop calling all other functions or stop wherever it is. For ex if drawMoon function is called and someone clicks on stop all other functions drawCircle1,drawCircle2 shouldnt be called.

  1. pause button also there would be third button which when clicked will pause the setTimeout function as above. when i click on same button again it should call function from where it has stopped.

Is this possible?

I am calling this function

function drawLayers() {

    //setTimeout(drawBlueSky,500);
    //setTimeout(drawCircle1,1250);
    setTimeout(drawMoon,800);
    setTimeout(drawCircle1,2300);
    setTimeout(drawCircle2,2700);
    setTimeout(drawCircle3,3100);
    setTimeout(drawCircle4,3500);
    setTimeout(drawCircle5,3900);
    setTimeout(drawtext2,4300);
    setTimeout(drawtext,4700);
    setTimeout(drawtext3,5100);
    setTimeout(drawtext4,5500);
    setTimeout(drawtext5,5900);
    setTimeout(drawtext6,6300);
    setTimeout(drawtext7,6700);
    setTimeout(drawtext8,7100);
    setTimeout(drawtext9,7500);
    setTimeout(drawtext10,7900);

}

which calls many other functions fox ex drawMoon,drawCircle1 etc I am calling drawLayers() function on click of play button. What I need is if someone clicks on stop button the setTimeout function should stop calling all other functions or stop wherever it is. For ex if drawMoon function is called and someone clicks on stop all other functions drawCircle1,drawCircle2 shouldnt be called.

  1. pause button also there would be third button which when clicked will pause the setTimeout function as above. when i click on same button again it should call function from where it has stopped.

Is this possible?

Share Improve this question edited Sep 22, 2011 at 13:32 Richard JP Le Guen 28.7k8 gold badges93 silver badges120 bronze badges asked Aug 25, 2011 at 14:04 satyasatya 331 silver badge3 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

Have such code:

var timers = [];
function drawLayers() {

    //setTimeout(drawBlueSky,500);
    //setTimeout(drawCircle1,1250);

    timers = [];
    timers.push(setTimeout(drawMoon,800));
    timers.push(setTimeout(drawCircle1,2300));
    timers.push(setTimeout(drawCircle2,2700));
    timers.push(setTimeout(drawCircle3,3100));
    timers.push(setTimeout(drawCircle4,3500));
    timers.push(setTimeout(drawCircle5,3900));
    timers.push(setTimeout(drawtext2,4300));
    timers.push(setTimeout(drawtext,4700));
    timers.push(setTimeout(drawtext3,5100));
    timers.push(setTimeout(drawtext4,5500));
    timers.push(setTimeout(drawtext5,5900));
    timers.push(setTimeout(drawtext6,6300));
    timers.push(setTimeout(drawtext7,6700));
    timers.push(setTimeout(drawtext8,7100));
    timers.push(setTimeout(drawtext9,7500));
    timers.push(setTimeout(drawtext10,7900));

}

function StopAll() {
    for (var i = 0; i < timers.length; i++)
         window.clearTimeout(timers[i]);
}

You could make a central var isStopped = false; variable which you set to true when stopping. Then make every function check for the variable before executing.

You can define a var corresponding to your setTimeout and then clear it if you want to cancel the timeout.
For instance:

// set a timeout
timer = setTimeout("testtimeout()",3000);   

// clear the timeout
clearTimeout(timer);

You can wrap all your timeouts within an array and loop over the array to cancel every timeout

[EDIT] seems like Exelian's answer is quite cleaner and more adapted to your code

you could create an array that contains all the timeour so you have a reference to remove all of them

have a look at the below:

http://greengeckodesign./blog/2007/10/how-to-clear-all-timeouts-in-javascript.html

The mon way to do this is using clearTimeout:

var myTimeout = setTimeout(function(){
    alert('hi');
}, 500);
clearTimeout(myTimeout); // No 'hi' alert for you, sir

But in your case, I would propose a more concise way of controlling all the timeouts:

var keepRunning = true;

var timeouts = {
    drawMoon: 800,
    drawCircle1: 2300,
    drawCircle2: 2700,
    drawCircle3: 3100,
    drawCircle4: 3500
    // etc...
};

for(var func in timeouts)
    var timeout = setTimeout(func, timeouts[func]);

// just demonstrating a timeout run function here, such as "drawMoon":
var drawMoon = function(){
    ...
    if(!keepRunning)
        return;
    ...
    // probably conditionally
    keepRunning = false;
    ...
};

Set your stop button to set a flag (ex. stopPressed = true;). I would write a function drawNextLayer(previousLayer).

Then write

while (stopPressed === false && previousLayer !== lastLayer) {
    drawNextLayer(previousLayer);
    previousLayer++;
}
发布评论

评论列表(0)

  1. 暂无评论