This question already has answers here:
Why does the setInterval callback execute only once?
(2 answers)
JS setInterval executes only once
(2 answers)
Closed 6 months ago.
Just as the title says: setInterval is only firing its callback once.
manifest.json:
{
//...
"content_scripts" : [{
"js" : ["code.js"],
//...
}],
//...
}
code.js (example):
setInterval(alert('only shown once'),2000);
Why, and how I could fix it? The code works well outside of an extension (even in a bookmarklet).
setInterval(function() { alert('only shown once') },2000);
You need to pass a function reference like alert and not a return value alert()
setInterval isn't working at all.
The first argument should be a function, you are passing it the return value of alert() which isn't a function.
Use the three argument version:
setInterval(function,time,array_of_arguments_to_call_function_with);
setInterval(alert,2000,['only shown once']);
The way you wrote it it's wrong:
setInterval() wants a function and a numerical value: setInterval(function(){//your code}, timeInterval).
Related
This question already has answers here:
Why can I use a function before it's defined in JavaScript?
(7 answers)
Closed 5 years ago.
I'm currently a teacher's assistant in a web development course. Today, a student asked for help with his homework, in which he'd used setInterval, passing as the first parameter a function which he didn't define until a few lines of code later. I told him that wouldn't work, as the function would be undefined by the time the interval setting code was reached.
To my surprise, it worked perfectly. I've been trying to research this and am coming up blank: does JavaScript actually wait until the first execution of the callback to even see if the function name passed to it exists? That seems so counter-intuitive, but I can't imagine any other reason it would have worked. Where can I find out more about this unexpected behavior?
It depends:
If its a function expression :
//callback not defined ( exists but undefined)
var callback=function(){};
//callback defined
If its a function declaration :
//callback is defined
function callback(){}
//callback is defined
This is called hoisting, so vars and functions are moved to the top.
It also depends on the passed function too:
setInterval(callback,0);//doesnt work, callback is *undefined* in this moment
setInterval(function(){ callback();},100);//does work as callback is just called before being referenced.
var callback=function(){};
This question already has answers here:
Referencing "this" inside setInterval/setTimeout within object prototype methods [duplicate]
(2 answers)
Closed 6 years ago.
I'm having issues with the window.setInterval() method. Below is an example of the structure, the method "repeat" is called repeatedly, however I cannot call any methods inside "repeat". In the example when I instantiate manager (let m = new manager()) it will print "Before Print", but will not print out the log from the printStuff method, or the "After Print" message.
Does anyone know why this is happening? Obviously this isn't my actual code as it's simple enough to not be in separate functions, however my actual code needs to call many functions in the "repeat" function and it will stop execution when it finds a call to another function.
class manager{
constructor(){
window.setInterval(this.repeat, 5000);
}
repeat(){
console.log("Before Print");
this.printStuff();
console.log("After Print");
}
printStuff(){
console.log("Print Stuff");
}
Set interval will take take the this.repeat out of the context you need to either explicitly 'bind' the method using
setInterval(this.repeat.bind(this), 5000)
or
setInterval(()=>this.repeat(), 5000)
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);
This question already has answers here:
Calling a function in JavaScript without parentheses
(7 answers)
Closed 9 years ago.
I have javascript script code here. When I execute my code as it is then it does not trigger func_two function. But when I change following code in func_one
if (this.remove) {
this.func_two;
}
to this
if (this.remove) {
this.func_two();
}
Then it does trigger second function. But I want to trigger it this way this.func_one. IS it possible to do it this way? How?
You have to put () when you call a function, you can't just, out of nowhere, decide that you want it to work another way.
Take a look at this answer, it may help you.
this.func_two;
This statement return the function. It does not call the function. To call the function you have to add () at the end. or you have to do it like:
f2=this.func_two;
f2();
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
setInterval() only running function once
I'm using the following script to understand the concept of function context. I'm excepting the alerts to start from 20 onwards and repeat. But it goes quite after displaying 20.
When using call method and providing the o2, it should get the value 20 and repeat from there on.
<script type="text/javascript">
var o2 = {
local: 20
}
var local=0;
function someFuncObject(){
alert('Thats method object again ' + this.local++);
}
// window.setInterval(someFuncObject, 2000); //This works perfect!!!
window.setInterval(someFuncObject.call(o2), 2000); // This does not, why?
</script>
On the otherhand if I use window.setInterval(someFuncObject, 2000); it works fine and repeats again and again. What's the problem?
someFuncObject.call(o2) actually calls the function. This means, what gets passed into setInterval is what is returned from someFuncObject(). What setInterval expects as parameter is a function itself, not what the function returns.
If you want to control the value of this inside someFuncObject, you can use an anonymous function like:
window.setInterval(function() {
someFuncObject.call(o2);
}, 2000);