简体   繁体   English

每'x'秒执行一次.map()函数

[英]Execute a .map() function every 'x' amount of seconds

I have a function: 我有一个功能:

function getPattern (sequence) {
  sequence.map(pod => pod.classList.add('playing'))
  // rest of code
}

How would I execute pod.classList.add('playing') every, let's say, 2 seconds? 我如何每隔2秒执行pod.classList.add('playing') Moreover, I want to keep this synchronous to ensure that //rest of code is run after all the pod iterations have finished. 此外,我想保持同步,以确保所有pod迭代完成后//rest of code得以运行。

(For those wondering, sequence is just an array of HTML nodes) (对于那些想知道的人,序列只是HTML节点的数组)

EDIT What I have tried: 编辑我尝试过的:

sequence.forEach(pod => setTimeout(() => { pod.classList.add('playing') }, 2000))
sequence.map(pod => setTimeout(() => { pod.classList.add('playing') }, 2000))

setTimeout(() => {
 sequence.map(pod => pod.classList.add('playing'))
}, 2000)
setInterval(() => {
 sequence.map(pod => pod.classList.add('playing'))
}, 2000)

However I am suffering both problems I wanted to avoid in the question: addClass isn't delayed; 但是,在这个问题上,我都想避免两个问题: addClass不会延迟; all iterations execute 'at the same time'. 所有迭代都“同时”执行。 Moreover, the // rest of code is being run asynchronously (aka I am noticing console.log 's immediately). 此外, // rest of code正在异步运行(也就是我立即注意到console.log )。

You can use Promise s to spread out adding the class to each node and also to run code after all nodes have been processed. 您可以使用Promise扩展将类添加到每个节点,还可以在处理完所有节点之后运行代码。

 var myArrayOfDomNodes = Array.from(document.querySelectorAll('p')); addClassesSequentially(myArrayOfDomNodes).then(_ => { // this code will run after all classes have been added. console.log('all done'); }); function addClassAfterTwoSeconds(el) { return new Promise(res => { setTimeout(_ => { el.classList.add('playing'); res(); }, 2000); }); } function addClassesSequentially(sequence) { let item = sequence.shift(); return item ? addClassAfterTwoSeconds(item).then(addClassesSequentially.bind(null, sequence)) : Promise.resolve(); } 
 p.playing { color : red; } 
 <p>One</p> <p>Two</p> <p>Three</p> <p>Four</p> 

You can't simply pause your script's execution, using setTimeout / setInterval is your only option. 您不能简单地暂停脚本的执行,使用setTimeout / setInterval是唯一的选择。 However since all setTimeout calls get executed the same time, you either have to nest them or call them with increasing timeouts (2s, 4s, 6s, ...). 但是,由于所有setTimeout调用都在同一时间执行,因此您必须嵌套它们或以越来越大的超时时间(2s,4s,6s等)来调用它们。 The latter can be achieved by calling the following: 后者可以通过调用以下命令来实现:

`sequence.forEach((pod, i) => setTimeout(() => { pod.classList.add('playing') }, (i + 1) * 2000))`

For execution the rest of the script after the last callback has finished, you have to wrap it in another setTimeout callback with a delay of sequence.length * 2000 (or (sequence.length + 1) * 2000 , depending on your needs). 为了在最后一个回调完成后执行脚本的其余部分,您必须将其包装在另一个setTimeout回调中,延迟为sequence.length * 2000 (或(sequence.length + 1) * 2000 ,具体取决于您的需要)。

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

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