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

时间秒杀一切 提交于 2019-12-10 14:31:44

问题


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 ?


回答1:


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.




回答2:


There's no reason to delay the onload event. Maybe you want to use window.load instead?

$(window).load(function(){
    //your code here
});


来源:https://stackoverflow.com/questions/7800048/how-to-delay-the-onload-event-which-gets-fired-on-the-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!