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
Related
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).
This question already has answers here:
setTimeout ignores timeout? (Fires immediately) [duplicate]
(3 answers)
Closed 3 years ago.
So, I need to make a function wait a time interval before executing, however, I need the rest of the code to execute while this wait is happening. Basically, I want to change variables in function of time passed without having to make all code wait for that to be executed.
I'll give an example so you can understand me better.
function example(){
sampleCode();
}
var x = 0;
if(x > 0){
console.log("enough time has passed")
}
example();
Take in mind that the whole block of code is being repeated multiple times a second, it's not a single execution program. I need to make x greater than 0 without preventing "example" from executing, so, setInterval is a nono (unless it has a functionality I'm not aware of). How can I do this? (Ignore the fact that x is being defined in this scope, so it's being set to 0 over and over, pretend it's a global variable).
EDIT: I've been recommended to use setTimeout and to show how I use it, here's how:
function handleMouseClick(evt){
[...]
setTimeout(test(), 3000);
}
function test(){
alert("Testing");
}
This results in an instant display of alert, no matter how much time i put into the timeout. What's wrong?
The issue you have is that you are not properly using the setTimeout function. You need to remove the parentheses from your call.
setTimeout(test, 3000);
Downvoters; I will make a more thorough and detailed answer for future readers later today when I’m at a computer. I am currently on break and do not have my computer with me. You can view some of my other answers to set your mind at ease that I will be back to flesh out a more detailed answer.
This question already has answers here:
setInterval/setTimeout return value
(6 answers)
Closed 5 years ago.
I am executing the following code snippet on browser console.
console.log(
setTimeout(function(){
console.log('a');
},200));
This gives me two outputs. First output is a random number(that is what i thought) and second is a. While I understand that the second output is normal, what is the first random number that is being generated in console.
The other number that is being logged is the return value of setTimeout function, which is the id for the timer, which can be used to clear the timer. See this for details
From MDN:
The returned timeoutID is a numeric, non-zero value which identifies the timer created by the call to setTimeout(); this value can be passed to clearTimeout() to cancel the timeout.
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout#Return_value
The 8 you are seeing is the return value of the call to setTimeout, which is passed to console.log and output.
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);
This question already has answers here:
Javascript function in setInterval
(3 answers)
Closed 8 years ago.
What is the difference in between:
k = setInterval(function(){loop();},100);
and
k = setInterval(loop(),100);
Why does the second one run once, like the setTimeout() function and the first one every 1/10 of a second?
In second one you are actually executing loop before setInterval is called, so you are passing result of the loop to the setInterval
More similar would be
k = setInterval(function(){loop();},100);
k = setInterval(loop,100);
first example allows you to do some work before calling loop and to pass some arguments if necessary
second one requires a function object as a 1st parameter and you cannot pass anything inside the brackets.
1st one is more widely spread pattern than 2nd (which is a little bit obsolete)
Lets checkout how doest JS run this code:
Fist way -
JS see setInterval - it should have 2 arguments and timer - in first variant everythink is trivial - you passed a function to be called on each interval and timer.
Lets check second variant - again setInterval, 2 params . But this time you dont pass link to function but called function and the result of this function will be passed to the setInterval. As you called function - it runs once, as it doesent return function setInterval cant run anything.