不要为此使用 setTimeout。
只需使用jquery ajaxsuccess
的回调功能:在回调或新done
助手中执行“之后”代码:
$.ajax({
url: "test.html"
}).done(function() {
// do things after the answer has been received
});
编辑 :
没有 jquery,也没有立即触发:
var httpRequest = new XMLHttpRequest();
var requestDoneAndOK = false;
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
requestDoneAndOK = true;
}
}
};
httpRequest.open('GET', url);
httpRequest.send();
当“注册”按钮被点击时,你只需要检查requestDoneAndOK
它是否真的调用你的其他函数。
编辑 2:
一旦实现 2 个条件,就会执行代码:收到答案并单击注册按钮。
var registerClicked = false;
var ajaxAnswerReceived = false;
function check() {
if (registerClicked && ajaxAnswerReceived) {
// do the thing !
}
}
var httpRequest = new XMLHttpRequest();
var requestDoneAndOK = false;
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
ajaxAnswerReceived = true;
check();
}
}
};
httpRequest.open('GET', url);
httpRequest.send();
document.getElementById('registerButton').onclick = function(){
registerClicked = true;
check();
};