开发者

to call a javascript function periodically

I want to call function with arguement periodically.

I tried开发者_C百科 setTimeout("fnName()",timeinseconds); and it is working.

But when I add an arguement it won't work. eg: setTimeout("fnName('arg')",timeinseconds);


You can add an anonymous function:

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


Use an anonymous function, like this:

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 being bad practice...e.g. the scope you're in when it runs.

Just as a side-note, if you didn't need an argument, it's just:

setTimeout(fnName, time);


setTimeout accepts an expression or a function name or an anonymous function but NO () operator.

() will start executing the function immediately and results in setTimeout accept an invalid parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜