How to create pause or delay in FOR loop?

前端 未结 12 1680
长情又很酷
长情又很酷 2021-02-01 19:57

I am working on a website, where I need to create a pause or delay.
So please tell me How to create pause or delay in for loop in javascript or

12条回答
  •  Happy的楠姐
    2021-02-01 20:23

    I am executing a function where I need access to the outside object properties. So, the closure in Guffa solution doesn't work for me. I found a variation of nicosantangelo solution by simply wrapping the setTimeout in an if statement so it doesn't run forever.

        var i = 0;
        function test(){
    
            rootObj.arrayOfObj[i].someFunction();
            i++;
            if( i < rootObj.arrayOfObj.length ){
                setTimeout(test, 50 ); //50ms delay
            }
    
        }
        test();
    

提交回复
热议问题