[英]jquery execute function while ajax response is loading
如何在客戶端等待服務器響應時執行將運行的函數? 這是我的代碼。 我查了一下,發現了一個.load()函數,但這又如何適應呢? 任何幫助都會很棒! 謝謝
$.ajax({
type: "POST",
url: "mail.php",
data: {name: name.val(), email: email.val(), phone: phone.val(), subject: subject.val(), message: message.val()}
}).done(function(){
alert("Your message was sent. We will be in contact with you shortly.");
window.location="index.html";
});
在調用$ .ajax()之后編寫的下一行代碼將在瀏覽器等待響應時運行。
所以:
$.ajax();
yourAwesomeFN();
XhttpRequests是異步的。 無需額外的工作。
你看過“beforeSend”參數嗎?
$.ajax({
type: "POST",
url: "mail.php",
data: {name: name.val(), email: email.val(), phone: phone.val(), subject: subject.val(), message: message.val()},
beforeSend: function(){
// Handle the beforeSend event
},
complete: function(){
// Handle the complete event
}
// ......
});
在JS中執行另一個函數時你不能執行一個函數,因為它是單線程的:你可以在之前和之后做一些事情 - 你是否正在尋找一種方法來設置一個消息/微調器在等待時顯示? 檢查$.ajax()
調用中的beforeSend選項:
$.ajax({
type: "POST",
url: "mail.php",
data: {name: name.val(), email: email.val(), phone: phone.val(), subject: subject.val(), message: message.val()}
beforeSend : function(){
// do your stuff here
}
}).done(function(){
alert("Your message was sent. We will be in contact with you shortly.");
window.location="index.html";
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.