setTimeout does not run when running while loop [duplicate] - javascript

This question already has answers here:
Why isn't setTimeout cancelling my loop?
(6 answers)
Closed 2 years ago.
I have this js snippet,
let checker={flag:0,dirtyFlag:false};
let i=0;
setTimeout(()=>{
console.log('value of checker updated');
checker.dirtyFlag=true;
checker.flag=1;
},2000)
while (true) {
console.log(i++);
if(checker.flag==1){
console.log(checker.dirtyFlag);
break;
}
}
but the code runs endless, whereas expected behavior is, it should stop after 2000ms.
how can i debug the above code.

The function that setTimeout calls is added to the call stack when the main call stack runs and completes. So, it will always run after the while loop (in your case it won't complete because you are in an endless loop)
You can see an example in the below image which shows how WEB API and Callback Queue works.

Related

Javascript setTimeout Recursive [duplicate]

This question already has answers here:
Why the function called by setTimeout has no callstack limit?
(2 answers)
Closed 5 months ago.
Consider this :
function some_function(){
axios.get(...)
.then(x=>{//handle})
.catch(e=>{//handle})
.then(()=>{
setTimeout(()=>{
some_function();
},5000);
}
I don't care aboout cancelation, I just care if this is gonna blow the stack up.
No, it won't cause the stack to grow. setTimeout() callback functions are called from the main event loop asynchronously, not from your function. Your function returns immediately after you make the axios.get() call (since that's also asynchronous, it doesn't wait for it, either).

Is there any affect of using set timeout with 0 seconds in the sequence of the program execution? [duplicate]

This question already has answers here:
Why is setTimeout(fn, 0) sometimes useful?
(19 answers)
Closed 2 years ago.
What should be the output of the following program and kindly explain the code as well.
console.log("first");
setTimeout(() => {
console.log("second");
}, 0);
console.log("third");
In this scenario, it should have the below output:
"first";
"third";
"second";
Detailed explanation is in the link: https://www.geekabyte.io/2014/01/javascript-effect-of-setting-settimeout.html
The output will be like this:
first third second
Reason: Actually this is the combination of both stack and the queue. Each statement will run in sequence but "SetTimeOut" will push the specific line in the stack within the queue that will be executed after the time. Although it has zero second, still due to stack, it will run after the next instruction.

setTimeout in javascript executing timeout code right away and not waiting [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 6 years ago.
I have the following line in my javascript code
setTimeout(reload(), 30000);
Which I expect to wait 30 seconds then call the reload function.
The issue is that the reload function is being called right away and not waiting for the timeout, why is the setTimeout calling the reload function right away and not waiting the specified amount of time? The setTimeout call is also being done in an onloadend FileReader function if that would make any difference.
setTimeout accepts a function as the first argument, unless reload() return a function to be run, you probably wanted
setTimeout(reload, 30000);

node.js: Is console.log() a function? [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 8 years ago.
Exactly, is console.log() a function? Why do these following two code snippets have different output?
function delay(x) {
console.log('Start of timeout');
return x;
};
setTimeout(function(){console.log('End of timeout');}, delay(5000));
console.log('Start to do something else');
.
function delay(x) {
console.log('Start of timeout');
return x;
};
setTimeout(console.log('End of timeout'), delay(5000)); // ???????
console.log('Start to do something else');
Yes, console.log is a function.
The first snippet uses the expected syntac for setTimeout. The second calls console.log inline, which returns undefined. That comes the first argument to setTimeout. That explains the different timing of when End of timeout appears.

What use is setInterval with a value of zero? [duplicate]

This question already has answers here:
Why is setTimeout(fn, 0) sometimes useful?
(19 answers)
Closed 9 years ago.
I have seen some JavaScript code that has the following:
var myFunc = function () {
timeout = setTimeout(myFunc, 0);
}
It seems that this would immediately recall the function.
What use case is there for this?
Read this.
In short, it "pauses" the JavaScript execution to let the rendering threads catch up. It gives the browser a chance to finish doing some none-JavaScript things that have been waiting to finish before attending to this new piece of JavaScript.

Categories