Applying delay between iterations of javascript for loop

前端 未结 2 1644
遥遥无期
遥遥无期 2020-12-17 01:13

Is it possible to apply a delay to successive iterations of a javascript for-loop using jQuery or underscore? I have a for-loop on my page that I am using to pop up growl no

2条回答
  •  孤城傲影
    2020-12-17 01:18

    I prefer to use self-invoking function that receives a number of iterations:

    (function loop(i) {          
       setTimeout(function () {   
    
          console.log('hello world'); // your code
    
          if (--i) loop(i); // iteration counter
       }, 5000) // delay
    })(10); // iterations count 
    

提交回复
热议问题