setInterval JS function doesn't work? [duplicate] - javascript

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

Related

How can i correctly pass a function to a eventListener? [duplicate]

This question already has answers here:
Javascript, addEventListener callback function executes immediately and only once? [duplicate]
(3 answers)
Closed 1 year ago.
I made a basic eventListener which creates an alert on button click. This one doesn't work (it calls the alert on the page load)
function handleClick(){
alert("hi!");
}
document.querySelector("button").addEventListener("click",handleClick());
this one works instead, but i don't understand why
document.querySelector("button").addEventListener("click",function(){
alert("hello");
});
with handleClick() you are executing the function when that statement is read. You need to pass in the function handleClick reference.
This:
document.querySelector("button").addEventListener("click",handleClick);

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

Javascript function timer. Not working as expected [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 5 years ago.
Problem: Hello i'm a little stuck as to why setTimeout() does not call the function specified after the time passed I've tried a few things but nothing seems to work
Solution: If anyone knows any other way to call a function after a specific time
Here is my code :
refreshStats: function(){
this.goldLabel.text = Math.floor(this.player.data.gold);
this.attackLabel.text = Math.floor(this.player.data.attack);
this.defenseLabel.text = Math.floor(this.player.data.defense);
if (this.player.questsDone.length > 0){
console.log(this.player.questsDone)
this.bpText.text = this.player.questsDone[this.player.questsDone.length-1];
setTimeout(this.FadeConsoleText(), 5000);
}
},
FadeConsoleText: function(){
console.log("log");
},
Current output:
"Quest"
"a"
wanted Solution output:
"Quest"
(wait then call function)
"a"
Thank you in advanced
You want to pass the function, not what the function returns to setTimeout:
setTimeout(this.FadeConsoleText, 5000);

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

jQuery function vs handlers [duplicate]

This question already has answers here:
what is the difference between calling function in JavaScript with or without parentheses ()
(4 answers)
Closed 9 years ago.
A simple question from someone trying to learn:
I have this:
$(function(){$("#topFlag").hover(changeFlag, setFlag ); });
function changeFlag(){
//some code
};
function setFlag(){
//somecode
};
And it's all working (now). But what I expected to use was:
$(function(){$("#topFlag").hover(changeFlag(), setFlag() ); });
What's the difference? Why doesn't changeFlag() (with the parens) work? Isn't this a function call? What if I wanted to pass a parameter to the function?
Thanks for any insights (or pointers to documentation I can read). I've already checked out:
http://api.jquery.com/hover/
http://www.w3schools.com/js/js_functions.asp
changeFlag is a function.
changeFlag() calls that function.
You need to pass the function. You don't need to call the function and pass its return value.
When you add braces after a function name , it executes the function
setFlag() ; // calls the function
But you want the function to fire , when you hover over the element
And not at the time of attaching the event
In javascript the functions are also variables, when you pass it as a parameter you want to send the function to execute, if you write this yourFunc() you would be sending the result of that function instead.
To send parameter I use this:
$(function(){$("#topFlag").hover(function(){changeFlag(param1, param2,...)}, function(){setFlag(param1, param2,...)}); });
this creates an anonym function that call your functions.

Categories