简体   繁体   English

setInterval不适用于数组

[英]setInterval does not work with an array

I'm trying to simplify my code by using an array but I don't know how to do it. 我正在尝试通过使用数组来简化代码,但是我不知道该怎么做。

So far my code is, 到目前为止,我的代码是

var items = ['html5', 'css', 'javascript', 'jquery'];
var itemLoop = function(i) {
if (items[i]) {
    console.log(items[i]);
    setInterval(function(){itemLoop(i+1);}
    , 4000);
    }
}
itemLoop(0);

But this doesn't return the items as I want them to, basically I want the items to be returned in console.log() but on a loop. 但这并没有返回我想要的项目,基本上我希望这些项目在console.log()返回,但是要循环执行。 Originally my code was, 本来我的代码是

setInterval(function() {
    setTimeout(function() {
            console.log("html5");
    }, 5000);
    setTimeout(function() {
            console.log("css");
    }, 10000);
    setTimeout(function() {
            console.log("javascript");
    }, 15000);
    setTimeout(function() {
            console.log("jquery");
    }, 20000);
}, 20000);

But I'm looking to add more than just 4 items, and creating multiple setTimeout() 's doesn't seem like the right way or the most practical way so I wanted to put the "items" in an array instead but I don't think I'm doing it right. 但是我想添加的项目不止4个,并且创建多个setTimeout()似乎不是正确的方法或最实用的方法,因此我想将“项目”放入数组中,但我不这样做我认为我做对了。

Any help is greatly appreciated, thanks. 非常感谢任何帮助,谢谢。

There's a few ways you can do this, but you need to be able to capture the state either by changing the array or holding onto an integer. 有几种方法可以执行此操作,但是您需要能够通过更改数组或保持整数来捕获状态。

This solution uses a single interval along with an index to loop through the array, printing as needed and then stopping the timer when past the end of the array. 该解决方案使用单个间隔和索引来遍历数组,根据需要打印,然后在数组结束时停止计时器。

 const items = ['html5', 'css', 'javascript', 'jquery']; let i = 0; const interval = setInterval(() => { console.log(items[i]); i += 1; if (i >= items.length) { clearInterval(interval) } }, 4000); 

setTimeout() is a little more practical and leads to more succinct code in this situation since you're only performing one action on each item. setTimeout()更加实用,在这种情况下会导致代码更简洁,因为您仅对每个项目执行一项操作。 You just need to increment time by the item index * some multiple. 您只需要增加项目索引的时间*一些倍数即可。 For example (I changed 4000 to 1000 to make it run a little faster): 例如(我将4000更改为1000,以使其运行更快):

 var items = ['html5', 'css', 'javascript', 'jquery']; items.forEach((c,i) => setTimeout(() => console.log(c), i*1000)) 

If you want the delay before the first item just change i*1000 to (i+1) * 1000 如果要在第一项之前延迟,只需将i*1000更改为(i+1) * 1000

If you want it to run in a perpetual loop 如果您希望它永久循环运行
Now setInterval() becomes more useful: 现在, setInterval()变得更加有用:

 var items = ['html5', 'css', 'javascript', 'jquery']; let i = 0; let interval = setInterval(() => console.log(items[i++ % items.length]), 1000) // some point in the future call clearInterval(interval) to stop 

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

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