jQuery 页面加载后进行 Ajax 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23473162/
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
Make Ajax call after page load
提问by Anil Sharma
How do we make ajax call after page load.
我们如何在页面加载后进行ajax调用。
I need to make a ajax call suppose after 10sec document has loaded.
我需要在 10 秒文档加载后进行 ajax 调用。
function loadajax(){
$.ajax({
url:'test',
data:"username=test",
type:"post",
success:function(){
//do action
}
});
}
$('document').load(function(){
setTimeout(function(){
loadajax();
},10000);
});
I am doing it in this way. But doesn't succeeded.
我就是这样做的。但是没有成功。
回答by Gone Coding
So many answers, all slightly different, but the shortest recommendedsyntax to run the function 10 seconds after DOM ready, while still retaining best practices, would be:
这么多答案,都略有不同,但在DOM ready10 秒后运行该函数的最短推荐语法是:
$(function(){
setTimeout(loadajax,10000);
});
As Blazemonger mentions below, you could simply put setTimeout(loadajax,10000);
at the end of the document, however that is not as flexible to change. jQuery DOM load code should always be in a jQuery DOM load event (i.e. $(function(){...});
)
正如 Blazemonger 在下面提到的,您可以简单地放在setTimeout(loadajax,10000);
文档的末尾,但是更改起来并不灵活。jQuery DOM 加载代码应该始终在 jQuery DOM 加载事件中(即$(function(){...});
)
回答by Blazemonger
1000 is in milliseconds -- ten seconds would be 10000. Also, you're looking for $(document).ready
:
1000 以毫秒为单位——十秒就是 10000。此外,您正在寻找$(document).ready
:
$(document).ready(function(){
setTimeout(function(){
loadajax();
},10000); // milliseconds
});
回答by djbielejeski
Try it like this
像这样试试
$(function(){
loadajax();
});
回答by TED
You can try this:
你可以试试这个:
$(document).bind("load", function() {
//code
});