[英]Jquery ajax wait before fetching page
地獄人...讓一個簡單的Ajax代碼...
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert(msg.html());
});
我正在等待5秒后嘗試獲取msg.html()
? 所以過程如下...
some.php
我們怎樣才能做到這一點?
使用setTimeout
,setTimeout()方法將在指定的毫秒數后調用一個函數或計算一個表達式。 該功能僅執行一次。
如果要獲取msg
的html
,則首先要確保msg
是有效的HTML字符串,並且在獲取.html()
之前,請先通過$(msg)
將其轉換為jQuery對象。
$.ajax({
method: "POST",
url: "some.php",
data: {
name: "John",
location: "Boston"
}
})
.done(function(msg) {
setTimeout(function() {
alert($(msg).html());
}, 5 * 1000);
});
$.ajax({
type : 'POST',
url : 'some.php',
data :{ name : 'John', location : 'Boston'},
beforeSend : function(){
//you can do whatever you want here (loading gif etc.) until some.php responds
},
complete : fuction(){
//Here is for after responding
},
success : function(data){
//data is the response of some.php, you can use the data to fill some elements in DOM etc.
}
});
setTimeout(function(){
//the codes you write here fires after 5 secs
},5000);
您可以將其傳遞給您想要的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.