Why does declaring var timer = setTimeout(etc.) automatically execute the function? [duplicate] - javascript

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 10 months ago.
Why does declaring var timer = setTimeout(etc.) automatically execute the setTimeout() function? And how would one pass a setTimeout() function as a variable without having the setTimeout() automatically execute?
var do_this = function(params){console.log("executes automatically, but function is only declared")};
var delay = 50;
var timeoutID = setTimeout(do_this(), delay) //executes automatically

Functions such as setTimeout require a callback-function that can be called the moment the timer expires. When you just provide a function, it is called immediately because it is never added to the callback-queue.
Thus, you should pass it a callback-function:
setTimeout( () => ... )

Related

JS setTimeout Not Delayed [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 3 years ago.
I'm trying to put a delay in front of an AJAX call.
var delay = 2000;
$("#c_name").on("keyup", function() {
var entered = $(this).val();
if (entered.length > 1) {
setTimeout(dosearch(entered), delay)
}
});
Fo some reason I can't seem to get setTimeout to take hold. It's performing the dosearch() function instantly.
How can I get this to delay properly? Yes JQuery 3.3.1 is loaded up top.
Answer: Both of these work:
setTimeout(() => dosearch(entered), delay)
setTimeout( function() { dosearch(entered) }, delay)

AngularJS - TypeError: fn is not a function [duplicate]

This question already has answers here:
JavaScript setTimeout() won't wait to Execute? [duplicate]
(3 answers)
Closed 4 years ago.
I want to make a function being called in a certain interval of time inside the constructor. But I don't want to write the function inside the $interval statement because it's a little big. So I wanted to make something like this:
this.$interval(this.getSomething(), 1000);
And the initialize it outside of the controller's constructor:
getSomething = function getSomething(){
...
};
But it gives me an error of
TypeError: fn is not a function When it seems to me as a function... Any suggestions?
Try changing your function declaration to this.
getSomething = function(){
...
};
and call your interval part like this.
this.$interval(function(){ this.getSomething() }, 1000);

Why does setInterval call my function once? [duplicate]

This question already has answers here:
JS setInterval executes only once
(2 answers)
Closed 4 years ago.
function start (argument){
alert("Starting Quiz!");
var time = setInterval( timer(), 1000);
}
function timer(){
console.log("Time: " + counter + " seconds");
}
I found that when setInterval( "timer()", 1000); works as intended repeatably calling my timer function, but when I don't use quotes "" the function is only called once. Why is that?
You're executing the function yourself. You should just pass the function:
var time = setInterval(timer, 1000);
When you passed a string, you were telling it to evaluate an expression in the global context.

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

how to use recursive setTimeout from within a method [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 8 years ago.
Im trying to use setTimeout() for a recursive function, inside a method. I'm assuming I'm getting something with the 'this' scope wrong though. Here's an example.
(function(scope){
function gamePlay(){
// this.initialize();
}
gamePlay.prototype.droidMovement = function(){
console.log('droid is moving');
this.droidShoot();
}
gamePlay.prototype.droidShoot = function(){
console.log('droidShoot() has been fired');
setTimeout(this.droidShoot,1000);
}
scope.gamePlay = gamePlay;
})(window);
var game = new gamePlay();
game.droidMovement();
Ive tried the setTimeout 3 ways.
//this way repeats the function 2 times then stops
setTimeout(this.droidShoot,1000);
//this gives me an error
setTimeout(function(){this.droidShoot()},1000);
//this fires the function in a loop but doesnt wait the second
setTimeout(this.droidShoot(),1000);
Can anyone please explain what I'm doing wrong? thanks so much in advance.
You may want to remove the setTimeout from within the definition of droidShoot and replace setTimeout(this.droidShoot, 1000) with setInterval(this.droidShoot, 1000.
setInterval will automatically call this.droidShoot repeatedly at a 1s interval

Categories