I wanted a JavaScript function to run 60 seconds after page is loaded.
After a little research I have made, I’ve found that setTimeout() is the solution.
So that’s what I did:
<body onLoad="setTimeout(postAction('news.reads', 'article'), 60000);">
Somehow, setTimeout does not work. After the page is loaded, there’s no need to wait 60 seconds because postAction() runs immediately.
Why is it happening? How to solve it? Are there alternatives to setTimeout() up there?
Thank you!
The correct way to do what you want in JS, ie setting a timeout after the page is loaded:
Instead of
window.onload = function(){setTimeout(postAction,60000);};, which will work, too, but cause a mem-leak in IE <9. That’s just for completeness’ sakeAnyway, the key line here is
setTimeout(postAction,60000);Update
After seeing the code you’re using, this is the easiest fix: