setInterval not working (firing only once) in Google Chrome extension
27,242
Solution 1
setInterval(function() { alert('only shown once') },2000);
You need to pass a function reference like alert
and not a return value alert()
Solution 2
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']);
Solution 3
The way you wrote it it's wrong:
setInterval()
wants a function and a numerical value: setInterval(function(){//your code}, timeInterval)
.
Author by
Camilo Martin
Remember: don't take things too seriously. Especially online.
Updated on May 31, 2020Comments
-
Camilo Martin about 4 years
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).
-
qwertymk over 12 yearspossible duplicate of JS setInterval executes only once
-
-
Camilo Martin over 12 yearsOh, you're right. There is a problem elsewhere and my attempt at checking it with alert was sloppy. There is a reference in the actual code.
-
Camilo Martin over 12 yearsOh, you're right. There is a problem elsewhere and my attempt at checking it with alert was sloppy. There is a reference in the actual code.
-
Camilo Martin over 12 yearsYou're right. There is a problem elsewhere and my attempt at checking it with alert was sloppy. There is a reference in the actual code.
-
J2N about 11 yearsCould you explain why this behaves the way it does? I'm relatively new to JavaScript and would like to learn as much as I can about it. @qwertymk
-
qwertymk about 11 years
-
J2N about 11 years@qwertymk Awesome! Thank you.