javascript 如何延迟在浏览器上触发的 onLoad 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7800048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delay the onLoad event which gets fired on the browser?
提问by kay am see
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 ?
我试图延迟被触发的 onLoad 事件。有没有办法在客户端严格使用 javascript 来延迟这个事件?
回答by Mic
If you put a script
tag at the end of the body
like:
如果你script
在body
类似的末尾放一个标签:
<body>
...
<script>
setTimeout(function(){
//deferred onload
}, 2000);
</script>
</body>
The function will be executed after 2 sec in this case
在这种情况下,该函数将在 2 秒后执行
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.
正如我在下面的评论中所说:你不应该依赖延迟。但是在某个事件上使用回调。如果不可能,可能是一个更好的坏解决方案,是将 setInterval 与一个函数一起使用,该函数每 X 毫秒检查一次您正在等待的事物是否存在,然后触发。
回答by Ryre
There's no reason to delay the onload event. Maybe you want to use window.load instead?
没有理由延迟 onload 事件。也许你想改用 window.load ?
$(window).load(function(){
//your code here
});