Home > Net >  make loop run endlesly starting from 1 when it reaches max
make loop run endlesly starting from 1 when it reaches max

Time:03-28

I have this loop which runs like I want it every 5 seconds updating, but I want it to start from beginning when it reaches 20 I tried if statement, but I guess I do something wrong,

I want I to be 1 when it is 20 without breaking the loop I tried

thank you

CodePudding user response:

As per the description and comments, the requirement is the loop should execute infinitely without breaking and should count from 1 -> 20 -> 1.

For this, following code can be used:

function task(profileIndex) {
    setTimeout(
        () => {
            currentPage = profileIndex   1; // For 0 it would be 1, for 19 it would be 20
            // Following line would take care of infinite loop
            task((profileIndex   1) % 20); // % 20 will take care of resetting it to 0 when (profileIndex   1) is 20. Otherwise, it won't do any manipulation.
        },
        500 // After every 500ms, the page would be updated.
    )
}

task(0)

CodePudding user response:

hahaha, it works man. Thank you very much :) had to add updateInfo in your code after i noticed that currentPage updates on live server ,but DOM update is not called. updated code

  • Related