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

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.

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

"Real" Sleep() Function without callback [duplicate]

This question already has answers here:
What is the JavaScript version of sleep()?
(91 answers)
Closed 6 years ago.
I know the time.sleep functiom from python. I just want to waot 3 seconds without making a callback. If i had to implemebt a callback i also had to hanfle global vars and more callbacks. I would like to have a function:
function getdata() {
console.log("hello");
Sleep(3000);
console.log("stackoverflow!");
return "my data";}
Please don't give me a solution for my specific example... i would also need sleep in while functions and other things. I searched a lot but i only found (in exanple setTimeout) functions with callback.
It doesn't exist. Javascript isn't designed this way. You need to use a callback and setTimeout. Sorry!

Can i call in javascript a javascript function that runs in background or asynchronously making browser not freeze? [duplicate]

This question already has answers here:
How to run javascript function in "background" / without freezing UI
(7 answers)
Closed 7 years ago.
function test() {
some_code;
}
function do_many_test() {
var i;
for(i=0;i<100;i++){
test();
}
some_code;
some_code;
some_code;
}
The function do_many_test() makes browser stuck for about ten seconds: is there a way to call it making it running in background while the execution flow continues to next code?
If you are using modern browser. you can use
webworker
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

Repeating a script using setInterval() [duplicate]

This question already has answers here:
setTimeout ignores timeout? (Fires immediately) [duplicate]
(3 answers)
Closed 8 years ago.
I'm working on a Chrome extension to fetch tweets and I figured that I could use the setInterval() function to make the script run every minute. First I tried giving it the function like this:
setInterval(myFunction(), interval);
But it would only execute my script once.
Then out of curiosity I tried declaring the function in the setInterval() function like so:
setInterval(function() {body of my function}, interval);
And that works, but is not a very pretty solution, does anybody any other way of doing this or am I just going to have to deal with it?
Just remove the brackets from the first call. The reason for this is that you need to pass in the function, not the result of the function (what it returns).
When you write the function's name with brackets, it calls the function. When you exclude the brackets, it simply refers to the function like a variable, and so you can pass in your function to the setInterval() function.
setInterval(myFunction, interval);

Categories

Resources