当满足两个条件时,我需要执行特定的函数mvFinishItUp()
。更具体地说,一个条件是$.ajax
的回调成功,另一个条件是代码的正常流程,直到它到达函数。有点:
$.ajax({
url: mv_finalUrl,
success: function (data) {
mvFinishItUp(data);
},
dataType: 'html'
});
/* here a lot more code, with animations and some delays */
mvFinishItUp(data) {
/* My function code */
/* But this code must only run after it has been called via the call back
and after all the other code has been ran as well */
}
因此,如果ajax回调更快,或者相反,该函数必须等待所有代码。关于如何实施这一点的任何想法?
我愿意改变脚本代码的整个概念,因为我相信ajax和函数本身之间的松散代码应该转到函数中......
答案 0 :(得分:6)
这是jQuery Deferred objects的完美用例。
从AJAX调用中删除success:
参数,稍后注册处理程序:
var jqxhr = $.ajax(...);
// do some other synchronous stuff
...
// and *then* register the callback
jqxhr.done(mvFinishItUp);
延迟对象在(事件已经完成事件之后)在AJAX事件上注册时,完美地(按设计)处理。
答案 1 :(得分:0)
这里我添加了第二个参数来检查回调检查
function mvFinishItUp(data, byCallback) {
var iscallback = byCallback || false; // if you don't send byCallback
// default will false
if(iscallback) {
// execute if called by callback
}
}
success: function (data) {
mvFinishItUp(data, true); // call with second parameter true
},
要在完成ajax并执行ajax和mvFinishItUp()
之间的所有代码后执行mvFinishItUp
,您可以执行以下操作:
var allFunctionExecuted = false; // global to detect all code execution
$.ajax({
url: mv_finalUrl,
success: function (data) {
mvFinishItUp(data, true);
},
dataType: 'html'
});
function func1() {
}
function func2() {
}
// some other code
function func3() {
allFunctionExecuted = true;
}
现在,
function mvFinishItUp(data, byCallback) {
var iscallback = byCallback || false; // if you don't send byCallback
// default will false
if(iscallback && allFunctionExecuted) {
// execute if ajax done
// and others code done
}
}
答案 2 :(得分:0)
也许这样的事情可以解决问题:
var _data = undefined;
$.ajax({
url: mv_finalUrl,
success: function (data) {
_data = data;
myFinishItUp(data); // call the function from here if it's faster
},
dataType: 'html'
});
/* here a lot more code, with animations and some delays */
function myFinishItUp(data) {
this.data = data; // store the data from the AJAX call or the code, whichever reaches first
// if the code reaches this before the AJAX call completes, data will be undefined
if(typeof this.wasCalled == "undefined") {
/* My function code */
/* But this code must only run after it has been called via the call back
and after all the other code has been ran as well */
this.wasCalled = true;
}
}(_data); // function that calls itself when the code gets to this point with a self-contained boolean variable to keep track of whether it has already been called
当代码流到达那一点时,我使用了一个自调用函数execute,但是如果它是从AJAX调用中调用的,它就不会执行。它会跟踪是否已使用自包含的布尔值调用它。
答案 3 :(得分:0)
尝试如下,(这只是伪代码)
var isAJAXDone = false, isFunctionCodeDone = false;
$.ajax({
//..
success: function () {
isAJAXDone = true;
mvFinishItUp(data, isAJAXDone, isFunctionCodeDone);
}
});
//..Your function code
//..Add this below the last line before the function ends
isFunctionCodeDone = true;
mvFinishItUp(data, isAJAXDone, isFunctionCodeDone);
//..
mvFinishItUp(data, isAJAXDone, isFunctionCodeDone ) {
if (isAJAXDone && isFunctionCodeDone) {
//Do your magic
}
}
答案 4 :(得分:0)
这是非常“丑陋”的代码,但您可以将其修改为不使用全局变量,所以这只是说明性的:
var ajaxExecuted = false,
codeExecuted = false;
$.ajax({
url: mv_finalUrl,
success: function (data) {
ajaxExecuted = true;
mvFinishItUp(data);
},
dataType: 'html'
});
/* here a lot more code, with animations and some delays */
codeExecuted = true;
mvFinishItUp(data) {
/* My function code */
if(ajaxExecuted && codeExecuted) {
/* But this code must only run after it has been called via the call back
and after all the other code has been ran as well */
}
}
我刚刚添加了两个标志:ajaxExecuted和codeExecuted,并在函数内部有一个if语句,用于检查这些标志的值,并仅在其中两个标志设置为true时执行。所以没有mather首先调用它,只有在两个标志设置为true时才会执行。
更简洁的方法可能是在对象中实现该函数,并使用属性而不是全局变量。