Home > Back-end >  This program may change the definition of setinterval or maybe not
This program may change the definition of setinterval or maybe not

Time:12-20

Here a == 3 and the condition was true but when the setinterval runs, the value of a will be 4 then, do while will check the condition and it will be a false condition, though this will not run.

I don't see any error in this.

setinterval was blocking the next condition which will be verified but it's an infinite loop which does not end so the checking of condition will not happen but do while was executed first without checking the condition and the inside if condition was true will execute the setinterval method but the confusion created in the definition of asynchronous function definition is that (it will be multi-threaded so the execution of both will run simultaneously it will hold the execution setinterval and execute the upcoming statements) by following the definition it will check condition first but this will prove wrong the definition of do while loop because in do while the condition was checked in 2nd iteration 1st iteration will be executed without checking the condition of the loop.

var a=3;

function ali() {
  console.log("testing and understanding setInterval method");
  a  ;
}

do {
  if (a == 3)
    setInterval(ali,1000);
} while (a == 3);

setInterval should run but being on asynchronous function it is running with no output. When running it it says heap stack is full in Node.js.

CodePudding user response:

JavaScript is not multi-threaded. You essentially have:

condition = true;
do {
  setInterval(some_function_that_changes_condition, 1000)
} while(condition);

You're right that this is an infinite loop.

This is like saying "I will continuously order pizzas until one arrives". So you start ordering pizza-after-pizza on the phone but you never answer the door when one of the pizzas actually arrives because you're busy making phone-calls to order more pizzas.

setInterval basically schedules a function to be run later, but nothing else can happen in JavaScript until you relinquish control of the JavaScript context by returning from the function that this code was written in. There is no way for JavaScript to interrupt execution of your loop to execute the callback. Maybe you're thinking that it might happen during one of the setInterval calls? But it won't.

In Node.js the timer may very well be scheduled and the thread that wants to invoke the callback function may be runnable, but the JavaScript engine itself is busy running your infinite loop and cannot be interrupted.

CodePudding user response:

The timer functions never guarantee that they would run precisely after the interval. It literally says that "I will execute it soon as I get the breathing space after the specified time".

What is breathing space? Every time a function runs, the JS engine waits for the function to return. This is the breathing space. It is at that moment the JS engine checks its "Event Loop" what it has to execute next. This is the only time your timer function may be scheduled to run.

The timer never runs before the long loop is over in the following example.

function test()  {

  console.log('Starting')
  
  setTimeout(()=> console.log('timer executed'), 1000)
  
  for (let i=1; i<=9999999999; i  );
  
  console.log('the loop is over');
  

}

test()

  • Related