Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
make loop run endlesly starting from 1 when it reaches max

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!