本文介绍了如何在达到一定条件后,1000毫秒让我的脚本暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的setInterval脚本重复记录Hello World的10倍。我想使它停止1秒钟,重复10次,然后再次启动并做处理之后永远。

I've got a setInterval script that repeats logging "Hello world" 10 times.I would like to make it stop for 1 second after repeating 10 times, then starting again and doing the process for ever.

下面是我:

var i = 0;
var x = setInterval(function(){
console.log("Hello world");
i++;
if(i >= 10){
i = 0;
stopInterval()
}
},1000);

var stopInterval = function(){
clearInterval(x);
setTimeout(function(){
//restart the interval, but how do I do???
},1000);
};

然而,它说stopInterval没有定义,我认为这是

However, it says stopInterval is not defined and I thought it was

推荐答案

因此​​你需要我们可以使用clearInterval和使用来自setInterval的返回的ID将其清除。

So you need to use clearInterval and use the id that is returned from setInterval to clear it.

function myTimer() {
    var i = 0,
    interval = setInterval(function(){
        console.log("Hello world");
        i++;
        if(i >=10){
            clearInterval(interval);
            window.setTimeout(myTimer, 1000);
        }
    },100);
}

https://developer.mozilla.org/en -US /加载项/ code_snippets /定时器

这篇关于如何在达到一定条件后,1000毫秒让我的脚本暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 05:16