How to create pause or delay in FOR loop?

前端 未结 12 1520
长情又很酷
长情又很酷 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条回答
  •  再見小時候
    2021-02-01 20:39

    The following code is an example of pseudo-multithreading that you can do in JS, it's roughly an example of how you can delay each iteration of a loop:

    var counter = 0;
    
    // A single iteration of your loop
    // log the current value of counter as an example
    // then wait before doing the next iteration
    function printCounter() {
        console.log(counter);
        counter++;
        if (counter < 10)
            setTimeout(printCounter, 1000);
    }
    
    // Start the loop    
    printCounter();
    

提交回复
热议问题