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

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

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

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.

How can I make a part of code wait without halting everything else in JavaScript? [duplicate]

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.

setInterval JS function doesn't work? [duplicate]

This question already has answers here:
Why is my function call that should be scheduled by setTimeout executed immediately? [duplicate]
(3 answers)
Closed 7 years ago.
I'm trying to call a function every x-seconds,
setInterval( alert("Hello"),2000);
But the alert function appear just for the first call,
You may try this:
setInterval(function() {
alert("Hello");
} ,2000);
It's not working like you thought. The alert fires/runs directly without interval because you've directly called it. To be more clerer, in your code:
setInterval(alert("Hello"),2000);
You are calling the alert("Hello") but you should pass a function (or bind) which you want to run on an interval, which is:
setInterval(function(){
// ...
}, 2000);
So the passed anonymous function will be called using the given interval (bind is another way but I've used a function/closure for simplicity to clarify you).

Categories