How to use setInterval function within for loop

前端 未结 6 1018
攒了一身酷
攒了一身酷 2020-11-27 16:00

I\'m trying to run multiple timers given a variable list of items. The code looks something like this:

var list = Array(...);

for(var x in list){
    setInt         


        
6条回答
  •  北海茫月
    2020-11-27 16:48

    You can combine forEach and setTimeout to loop over the array with the interval.

    let modes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    let interval = 1000; //one second
    modes.forEach((mode, index) => {
    
      setTimeout(() => {
        console.log(mode)
      }, index * interval)
    })

提交回复
热议问题