我想在ajax響應到來之前顯示錯誤消息。試過用下面的代碼:在收到ajax響應之前顯示消息
$.ajax({
url: "user.php",
type: 'POST',
data: $('#forgot-form').serialize(),
beforeSend: function(){
$('#error').html("Please wait until your request is processed.");
setTimeout(function() { $("#error").html(''); }, 8000);
}
}).done (function(data) {
/* I tried with the commented code also. But this section didn't worked */
//$('#error').html("Please wait until your request is processed.");
// setTimeout(function() { $("#error").html('Please wait until your request is processed.'); }, 8000);
if (data == "success") {
$('#user-login').val('');
$('#error').html("Please check your email to reset your password");
setTimeout(function() { $("#error").html(''); }, 5000);
} else if (data == "invalid") {
$('#error').html("Username or email doesnot exist");
setTimeout(function() { $("#error").html(''); }, 5000);
$('#user-login').focus();
return false;
}
});
在user.php的頁面,如果用戶信息是正確的,我發送電子郵件給用戶。所以迴應只會遲到。直到那時我想顯示等待消息。
此代碼正確顯示等待消息。但之後有一個小小的延遲,然後只顯示成功消息。我需要避免這個時間延遲。如何更改代碼以避免延遲?
任何人都可以幫助我做到這一點?提前致謝。
@ awlad ..非常感謝。有效。 – Jenz 2014-10-29 09:45:55
@Jenz你好歡迎:) – 2014-10-29 09:48:39