繁体   English   中英

停止并启动setInterval()

[英]Stop and start setInterval()

我试图停止循环,以便其他事情可以运行,然后在顺序中的正确时间再次返回循环。 我已经执行了一次代码,但是在我使用clearInterval();之后似乎无法运行clearInterval();

function runPlanner() {

    var plannerInterval = setInterval(function(){

        imageCounter++;

        var imageTotal = $('li img').length;

        if (imageCounter > imageTotal + 1) {
            $('li img').hide();
            imageCounter = 0;
            clearInterval(plannerInterval);
        } else {
            $('li img:nth-child(' + imageCounter +')').fadeIn('slow');
        }

        console.log('image counter:',imageCounter);
        console.log('image total:',imageTotal);
    }, 2500);

}

然后,我可以使用以下命令进行初始运行:

runPlanner();

但是,如果满足条件(在其中调用clearInterval),则无法使用该函数调用。 任何想法,请!

尝试使用setTimeout样式。 因此您不再需要clearInterval的问题。

像这样。

function runPlanner() {

        imageCounter++;

        var imageTotal = $('li img').length;

        if (imageCounter > imageTotal + 1) {
            $('li img').hide();
            imageCounter = 0;
        } else {
            $('li img:nth-child(' + imageCounter +')').fadeIn('slow');

            // re-called `runPlanner` after 2500 sec
            setTimeout(runPlanner, 2500);
        }

        console.log('image counter:',imageCounter);
        console.log('image total:',imageTotal);

}

runPlanner();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM