I need to run a javascript function each 10 seconds.
I understand the syntax must work like follow but I am not getting any success:
function funcName() {
alert("test");
}
var func = funcName();
var run = setInterval("func",10000)
But this isn't working. Any help?
This question is related to
javascript
setinterval
That's because you should pass a function, not a string:
function funcName() {
alert("test");
}
setInterval(funcName, 10000);
Your code has two problems:
var func = funcName();
calls the function immediately and assigns the return value."func"
is invalid even if you use the bad and deprecated eval-like syntax of setInterval. It would be setInterval("func()", 10000)
to call the function eval-like.Change setInterval("func",10000)
to either setInterval(funcName, 10000)
or setInterval("funcName()",10000)
. The former is the recommended method.
Try this:
function funcName() {
alert("test");
}
var run = setInterval(funcName, 10000)
Source: Stackoverflow.com