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

204
Views
nodejs while vs setInterval

const x = {value: 1};

function plus(obj){
    setTimeout(() => {
        obj.value++; 
        console.log("timeout...");
    }, 3000);
};

plus(x)

setInterval(()=>{
    if(x.value==1){
       console.log(`x=${x.value}`);
    }
},500)


//while(x.value == 1){
//    console.log(`not yet!! x = ${x.value}`)
//}

The function plus adds 1 after 5 sec and setInterval keep print log until x.value is not 1.

It works as I intended.

But while loop which I annotated prints log infinity.

I can't find any difference between setInterval and while except setInterval do this every 0.5 sec.

Can anyone explain why while loop never ends?

( I think while loop should end after 3 secs because the function plus changes value of x by adding 1, so condition in while loop is false after 3 secs )

about 4 years ago · Juan Pablo Isaza
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!