3

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!

Ido
  • 397
  • 2
  • 7
  • 22
  • 2
    Umm some code samples please... – Martin Nov 06 '12 at 22:14
  • 1
    It could be anything: are you passing `postAction` or `postAction()` as a first argument to `setTimout`? the second is _wrong_. Are you passing a string as a first argument? Don't. The timeOut is specified in milliseconds, not seconds: 60 seconds is, thus, written as 60000 – Elias Van Ootegem Nov 06 '12 at 22:17
  • Sorry, I posted my code earlier, but forgot to mark it as "code", so it was removed... Now I added the code sample again. – Ido Nov 06 '12 at 22:26
  • 2
    You're not calling the function after 60 seconds with your snippet, you're calling _the return value of the function_: `postAction(argument, argument)` is a function call, whatever it returns is what `setTimeout` will try to execute after 60 seconds. Most likely, this will be undefined – Elias Van Ootegem Nov 06 '12 at 22:26

4 Answers4

11

You need to wrap postAction in a function to defer execution:

setTimeout(function() { postAction('news.reads', 'article'); }, 60000);

You are actually executing postAction immediately, your code is equivalent to:

var result = postAction('news.reads', 'article');

setTimeout(result, 60000);
Mike Valenty
  • 8,941
  • 2
  • 30
  • 32
0

Are you using setTimeout like :

setTimeout(function(){alert("OK");}, 1000 * 60); // alert "OK" in 60s
ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54
0

The correct way to do what you want in JS, ie setting a timeout after the page is loaded:

(function(w)
{
    var load = function()
    {
         setTimeout(postAction,60000);
         if (w.removeEventListener)
         {//remove listeners, to avoid leak...
             return w.removeEventListener('load',load,false);
         }
         return w.attachEvent('onload',load);
    };
    if (w.addEventListener)
    {
        return w.addEventListener('load',load,false);
    }
    return w.attachEvent('onload',load);
}(this));

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' sake
Anyway, the key line here is setTimeout(postAction,60000);

Update
After seeing the code you're using, this is the easiest fix:

<body onLoad="setTimeout(function(){ return postAction('news.reads', 'article');}, 60000);">
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
-2
<script>
        function doSomeJavascript() {
            // javascript code goes here
           alert('5 secs is up!');
        }
        // calls function doSomeJavascript() 5 secs after page load
        var interval = setInterval(doSomeJavascript, 5000);

        setTimeout(function() {
           window.clearInterval(interval);
        }, 5000);
</script>
Martin
  • 10,294
  • 11
  • 63
  • 83