Node.js setTimeout in forever loop [duplicate] - javascript

This question already has answers here:
Callback of an asynchronous function is never called
(1 answer)
setTimeout not working inside infinite loop
(5 answers)
Closed 5 years ago.
Can't explain nodejs behavior. I have code:
while (true) {
setTimeout(() => console.log(1), 0)
}
And this script just hanging...
How come? I've been thinking setTimeout is non-blocking and asynchronous, and nodejs use timers event loop phase for scheduling setTimeout callbacks... but it seems like event loop blocked...

Your while loop is infinite. It will just keep setting timeouts and never exiting the loop. Since JavaScript is single-threaded, the code for the timeouts won't run until the current code is finished, and since the while loop never finishes, the timeouts don't run.
If you want to spam the console with the number 1 using timeouts, which it looks like you are trying to do, you would have to set the next timeout in the current timeout's callback:
function timeoutFunc() {
console.log(1);
setTimeout(timeoutFunc, 0);
}
timeoutFunc();

Related

Using recursive setTimeout in Javascript - how to avoid increasing call stack? [duplicate]

This question already has answers here:
How does setTimeout prevent potential stackoverflow
(2 answers)
Why the function called by setTimeout has no callstack limit?
(2 answers)
Closed 5 years ago.
By using javascript recursive setTimeout function, is it risky to get the stackoverflow?
By trying this example you can see in browser console that the stack grows. Why is it so?
var iteration = 0;
function bar() {
iteration++;
console.log("iteration: " + iteration);
console.trace();
if(iteration < 5){
setTimeout(bar, 5000);
}
}
bar();
By using javascript recursive setTimeout function, is it risky to get the stackoverflow?
No. setTimeout registers a handler that will get called by the browser when the timer triggers. By the time that happens, the stack has unwound (if it hadn't, the task that scheduled the timeout wouldn't have ended, and the browser's UI would be locked up waiting for it to end).
By trying this example you can see in browser console that the stack grows.
No, the stack unwinds before the handler is next called. If you're referring to the async "stack" entries that Chrome's devtools show you, those aren't the real stack, and they're a devtools artifact. (Start your timer, watch for two ticks so you see the "async" entry on the second one, then close your console, wait two more ticks, and reopen it; notice that there aren't any "async" entries — at all, not even the one you saw it log before closing the console!)

By using javascript recursive setTimeout function, is it risky to get the stackoverflow? [duplicate]

This question already has answers here:
How does setTimeout prevent potential stackoverflow
(2 answers)
Why the function called by setTimeout has no callstack limit?
(2 answers)
Closed 5 years ago.
By using javascript recursive setTimeout function, is it risky to get the stackoverflow?
By trying this example you can see in browser console that the stack grows. Why is it so?
var iteration = 0;
function bar() {
iteration++;
console.log("iteration: " + iteration);
console.trace();
if(iteration < 5){
setTimeout(bar, 5000);
}
}
bar();
By using javascript recursive setTimeout function, is it risky to get the stackoverflow?
No. setTimeout registers a handler that will get called by the browser when the timer triggers. By the time that happens, the stack has unwound (if it hadn't, the task that scheduled the timeout wouldn't have ended, and the browser's UI would be locked up waiting for it to end).
By trying this example you can see in browser console that the stack grows.
No, the stack unwinds before the handler is next called. If you're referring to the async "stack" entries that Chrome's devtools show you, those aren't the real stack, and they're a devtools artifact. (Start your timer, watch for two ticks so you see the "async" entry on the second one, then close your console, wait two more ticks, and reopen it; notice that there aren't any "async" entries — at all, not even the one you saw it log before closing the console!)

how to run sequence functions in javascript [duplicate]

This question already has an answer here:
How can I write an async method in JavaScript when posting or looping? [closed]
(1 answer)
Closed 9 years ago.
How to run sequence functions in JavaScript:
I've 4 functions like this:
function myfunction()
{
callfunction1();
callfunction2();
}
In callfunction1:
function callfunction1()
{
callfunction3();
}
My problem is in "myfunction" when I call "callfunction1",callfunction1 is run and it call "callfunction3" but when "callfunction3" haven't finish,"callfunction2" is running.Have anyway to wait "callfunction3" finish then "callfunction2" is run ?
JavaScript is single-threaded, callfunction2 will only run after callfunction3 has compeletely finished.
However, if callfunction3 contains some AJAX or other asynchronous operation, then that will always happen after callfunction2 (because of the single-threaded thing, it can't start the asynchronous callback until after the synchronous stuff is done). Anything that relies on the result of an asynchronous function MUST either be in the function itself, or called by said function.

Change interval of setInterval [duplicate]

This question already has answers here:
Changing the interval of SetInterval while it's running
(17 answers)
Closed 10 years ago.
Is there a way of modifying the interval of calls of function set with setInterval during runtime, other than removing it (clearInterval) and reinstating again with a different value?
Use setTimeout instead, additionally this a non-blocking method for async JS:
var interval = 1000;
function callback() {
console.log( 'callback!' );
interval -= 100; // actually this will kill your browser when goes to 0, but shows the idea
setTimeout( callback, interval );
}
setTimeout( callback, interval );
Don't use setInterval, as in some cases (lots of setInterval + long callbacks, which are usually longer than timeout), due to limited queue size, some callbacks will be dropped by the browser and never executed. Only setTimeout guarantees execution.
Nope; removing the interval and re-adding it is the way to do it if you've used setInterval().
You could accomplish the same goal of a varying timeout, however, by calling setTimeout() repeatedly with a variable delay at the end.
Out of curiosity, what are you doing that you want to modify the interval? Perhaps requestAnimationFrame() might be more appropriate?

Is there a Sleep/Pause/Wait function in JavaScript? [duplicate]

This question already has answers here:
What is the JavaScript version of sleep()?
(91 answers)
Closed 3 years ago.
Is there a JavaScript function that simulates the operation of the sleep function in PHP — a function that pauses code execution for x milliseconds, and then resumes where it left off?
I found some things here on Stack Overflow, but nothing useful.
You need to re-factor the code into pieces. This doesn't stop execution, it just puts a delay in between the parts.
function partA() {
...
window.setTimeout(partB,1000);
}
function partB() {
...
}
You can't (and shouldn't) block processing with a sleep function. However, you can use setTimeout to kick off a function after a delay:
setTimeout(function(){alert("hi")}, 1000);
Depending on your needs, setInterval might be useful, too.
setTimeout() function it's use to delay a process in JavaScript.
w3schools has an easy tutorial about this function.

Categories

Resources