Javascript setTimeout函数 [英] Javascript setTimeout function

查看:56
本文介绍了Javascript setTimeout函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用此函数

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);

}

调用了许多其他函数,例如 drawMoon drawCircle1 等单击播放按钮时,我正在调用 drawLayers()函数.我需要的是,如果有人单击停止按钮,则 setTimeout 函数应该停止调用所有其他函数,或者无论在何处都停止.例如,如果调用 drawMoon 函数并且有人单击停止所有其他函数 drawCircle1 ,则不应调用 drawCircle2 .

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. 暂停按钮,还有第三个按钮,单击该按钮将暂停上述setTimeout函数.当我再次单击同一按钮时,应该从停止的地方调用函数.

这可能吗?

推荐答案

具有这样的代码:

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]);
}

这篇关于Javascript setTimeout函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆