Call javascript function periodically

I want to periodically call a function with an argument.

I have tried setTimeout("fnName()",timeinseconds); and it works.

But when I add an argument, this will not work. eg:setTimeout("fnName('arg')",timeinseconds);

+5
source share
3 answers

You can add an anonymous function:

setTimeout(function() { fnName("Arg"); }, 1000);
+12
source

Use an anonymous function, for example:

setTimeout(function() { fnName('arg'); }, time);

In general, never pass a string to setTimeout()or setInterval(), if you can avoid it, there are other side effects besides bad practice ... for example. the area you are in when it works.

As a note, if you don't need an argument, simply:

setTimeout(fnName, time);
+2
source

setTimeout , < ().

() will start executing the function immediately and cause setTimeout to accept an invalid parameter.

0
source

All Articles