Javascript 如何延迟调用javascript函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5570017/
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 calling of javascript function?
提问by sams5817
I'm new to JavaScript.
我是 JavaScript 的新手。
I would like to call JavaScript / jQuery function after the page load in aspx page.
我想在 aspx 页面中加载页面后调用 JavaScript / jQuery 函数。
I tried using <form onload="function()">
and window.load = function(){}
, but the JavaScript is still trigger before some of the content fully loaded.
我尝试使用 <form onload="function()">
and window.load = function(){}
,但在某些内容完全加载之前,JavaScript 仍然会被触发。
Is it possible to call during Page_PreRender
in aspx page and not in code behind, so that I can delay the JavaScript function ?
是否可以Page_PreRender
在 aspx 页面中调用而不是在后面的代码中调用,以便我可以延迟 JavaScript 函数?
I tried setTimeout("function()",5000)
to solve the problem.
我试图setTimeout("function()",5000)
解决这个问题。
However setTimeout()
seem like is not compatible with some browser, e.g: caused looping in Google Chrome.
但是setTimeout()
似乎与某些浏览器不兼容,例如:导致 Google Chrome 中的循环。
回答by mplungjan
setTimeout
is compatible with all browsers since 1996. You should avoid the evaluation of "functionName()" and instead do:
setTimeout
自 1996 年以来与所有浏览器兼容。您应该避免评估“functionName()”,而是执行:
setTimeout(functionName,5000)
UPDATE:If you initially expect a variable passed to the function and none when in the timeout, you need to do this instead:
更新:如果您最初希望将变量传递给函数,而在超时时没有变量,则需要执行以下操作:
setTimeout(function() { functionName() },5000)
However you are calling the onload
incorrectly, so you need to do either this:
但是,您调用的是onload
错误的,因此您需要执行以下任一操作:
window.addEventListener("load",function() {
// your stuff
}
or the simpler
或者更简单的
window.onload=function() {
// your stuff
}
or, since you are using jQuery, this:
或者,由于您使用的是jQuery,因此:
$(document).ready(function() {
// your stuff
});
or just this:
或者只是这个:
$(function() {
// your stuff
});
回答by Oscar Godson
If you want to be 100% sure that it's when the page ACTUALLY loads, use:
如果您想 100% 确定它是在页面实际加载时,请使用:
$(window).load(function(){
//After EVERYTHING loads, including images.
})
The other's solution, onload
works, but it loads once the DOM is ready, but not when the window is actually finished loading.
另一个解决方案onload
有效,但它会在 DOM 准备好后加载,但不会在窗口实际完成加载时加载。
回答by Ken Browning
If you're going to be using jQuery then it's preferable to attach an event to the document ready event using one of the following:
如果您打算使用 jQuery,那么最好使用以下方法之一将事件附加到文档就绪事件:
$(document).ready(callback);
or
或者
$(document).ready(function () { /* do stuff */ });
or
或者
$(callback);
or
或者
$(function () { /* do stuff */ });
回答by MadBender
$(document).ready(function(){
//Code goes here
});
or old style
或旧风格
<body onload="myFunction()">
回答by Karsten
Would be best to use a framework like jQuery.
In jQuery you can define a function like this:
最好使用像jQuery这样的框架。
在 jQuery 中,您可以定义这样的函数:
$(document).ready(function() {
});
This way it will be called when the DOM is fully loaded.
这样它就会在 DOM 完全加载时被调用。
A full documentation of the ready() function can be found here.
可以在此处找到 ready() 函数的完整文档。