How to delay the onLoad event which gets fired on the browser?

19,749

If you put a script tag at the end of the body like:

<body>

  ...

  <script>
    setTimeout(function(){
      //deferred onload
    }, 2000);
  </script>
</body>

The function will be executed after 2 sec in this case

As said in my comment below: you shouldn't rely on a delay. But use a callback on a certain event. If impossible, may be a better bad solution, is to use setInterval with a function that check every X msec if the thing you are waiting for is present, and fire.

Share:
19,749
kay am see
Author by

kay am see

Updated on June 25, 2022

Comments

  • kay am see
    kay am see almost 2 years

    I am trying to delay the onLoad event that gets fired. Is there a way to delay this event strictly using javascript on the client side ?

  • kay am see
    kay am see over 12 years
    nope that does not work. setTimeout does not hold back the onload event to be fired.
  • kay am see
    kay am see over 12 years
    i want to delay the onload event.
  • Mic
    Mic over 12 years
    Sorry it was not clear, don't use the onload event anymore, but replace it with this technique to delay the start of your code.
  • Ryre
    Ryre over 12 years
    What's the reason for delaying the onLoad event? It seems to me like it'd be better to wait for onload, then delay your code. There's something I'm missing here.
  • Mic
    Mic over 12 years
    @Kunal you shouldn't rely on a delay. But use a callback on a certain event. If impossible, may be a better bad solution, is to use setInterval with a function that check every X msec if the thing you are waiting for is present, and fire.