Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 924
kay am see Avatar asked Oct 17 '11 21:10

kay am see


People also ask

How do I add a delay to window onload?

No, there isn't a way to delay the window onload. You could try to add a lot of images and other things that take a long time to load, but that's not an ideal solution. Instead, you can use setTimeout to make code run after a period of time. setTimeout(function(){ window.

Which event can be fired when document is loaded?

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded , which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading.

Which event attribute is fired when the element is loaded?

The onload attribute fires when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

What is a window onload and Ondocumentready?

ready() is a jQuery event which occurs when the HTML document has been fully loaded, while the window. onload event occurs later, when everything including images on the page loaded. Also window. onload is a pure javascript event in the DOM, while the $(document). ready() event is a method in jQuery.


1 Answers

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.

like image 135
Mic Avatar answered Sep 28 '22 16:09

Mic