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?
A lot of other answers are focusing on a pattern that does work, but their explanations aren’t really very thorough as to why your current code doesn’t work.
Your code, for reference:
Let’s break this up into chunks. Your function
funcNameis fine. Note that when you callfuncName(in other words, you run it) you will be alerting"test". But notice thatfuncName()— the parentheses mean to “call” or “run” the function — doesn’t actually return a value. When a function doesn’t have a return value, it defaults to a value known asundefined.When you call a function, you append its argument list to the end in parentheses. When you don’t have any arguments to pass the function, you just add empty parentheses, like
funcName(). But when you want to refer to the function itself, and not call it, you don’t need the parentheses because the parentheses indicate to run it.So, when you say:
You are actually declaring a variable
functhat has a value offuncName(). But notice the parentheses.funcName()is actually the return value offuncName. As I said above, sincefuncNamedoesn’t actually return any value, it defaults toundefined. So, in other words, your variablefuncactually will have the valueundefined.Then you have this line:
The function
setIntervaltakes two arguments. The first is the function to be ran every so often, and the second is the number of milliseconds between each time the function is ran.However, the first argument really should be a function, not a string. If it is a string, then the JavaScript engine will use
evalon that string instead. So, in other words, your setInterval is running the following JavaScript code:However,
funcis just a variable (with the valueundefined, but that’s sort of irrelevant). So every ten seconds, the JS engine evaluates the variablefuncand returnsundefined. But this doesn’t really do anything. I mean, it technically is being evaluated every 10 seconds, but you’re not going to see any effects from that.The solution is to give
setIntervala function to run instead of a string. So, in this case:Notice that I didn’t give it
func. This is becausefuncis not a function in your code; it’s the valueundefined, because you assigned itfuncName(). Like I said above,funcName()will call the functionfuncNameand return the return value of the function. SincefuncNamedoesn’t return anything, this defaults toundefined. I know I’ve said that several times now, but it really is a very important concept: when you seefuncName(), you should think “the return value offuncName“. When you want to refer to a function itself, like a separate entity, you should leave off the parentheses so you don’t call it:funcName.So, another solution for your code would be:
However, that’s a bit redundant: why use
funcinstead offuncName?Or you can stay as true as possible to the original code by modifying two bits:
In this case, the JS engine will evaluate
func()every ten seconds. In other words, it will alert"test"every ten seconds. However, as the famous phrase goes,evalis evil, so you should try to avoid it whenever possible.Another twist on this code is to use an anonymous function. In other words, a function that doesn’t have a name — you just drop it in the code because you don’t care what it’s called.
In this case, since I don’t care what the function is called, I just leave a generic, unnamed (anonymous) function there.