Javascript setTimeout Recursive [duplicate] - javascript

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).

Related

JavaScript function execution time [duplicate]

This question already has answers here:
Why does a setTimeout delay of 0 still run after all other synchronous code in a for loop? [duplicate]
(2 answers)
Closed 1 year ago.
I wanted to test the execution time of console.log, but whatever number I set as the delay for setTimeout, it is still executed after console.log. I don't understand why so.
function first() {
console.log("This should be printed first");
}
setTimeout(first, 0.00001);
console.log("This should be printed second");
From MDN:
delay Optional The time, in milliseconds that the timer should wait before the specified function or code is executed. If this parameter is omitted, a value of 0 is used, meaning execute "immediately", or more accurately, the next event cycle. Note that in either case, the actual delay may be longer than intended; see Reasons for delays longer than specified below.
No matter how small the delay, the first function is going to be put on a queue and not looked at until the current event cycle (which is in the middle of running the current function) has finished (which won't be until after the last console.log statement has been executed).
Further reading:
Concurrency model and the event loop
JavaScript Event Loop

setTimeout does not run when running while loop [duplicate]

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.

Can we execute callback to JavaScript's forEach asynchronously? [duplicate]

This question already has answers here:
JavaScript, Node.js: is Array.forEach asynchronous?
(12 answers)
Closed 5 years ago.
var a = [1,2,3,4,5,6,7,8,9,10];
a.forEach(item => {
...
});
console.log('all done');
Is it ever possible in browser or node environment that it console 'all done' before the forEach completes its execution? If yes then when?
No. Your code is synchronous so it can never happen unless you explicitly execute the .forEach function in an async block.
No. forEach is a synchronous function.
It is possible to call asynchronous functions from within it, and they might not have completed, but the loop itself will have done.

$.when().done() is firing the done function before when completes [duplicate]

This question already has answers here:
How do you work with an array of jQuery Deferreds? [duplicate]
(4 answers)
Closed 6 years ago.
I have a page like this:
$(document).ready(function() {
$.when(ajaxcall1(),ajaxcall2(),ajaxcall3()).done(finalCall());
});
For some reason the finalCall() is firing simultaneous to the three calls surrounded by $.when().
I tried calling a reference to the finalCall() function as in:
$(document).ready(function() {
$.when(ajaxcall1(),ajaxcall2(),ajaxcall3()).done(finalCall);
});
But even then, it still fires the function before the prior 3 complete.
NOTE: I am not including the functions here as they are not relevant. I just need to know why the finalCall() function would fire simultaneous to then $.when() functions.
Thank you.
$.when does not call your callback at all. You are doing it yourself:
// vv
$.when(ajaxcall1(),ajaxcall2(),ajaxcall3()).done(finalCall());
// ^^
Change that to
$.when(ajaxcall1(),ajaxcall2(),ajaxcall3()).then(finalCall);
where the function is actually passed into the promise method, and it'll work (assuming your ajax functions return promises).

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);

Categories