Javascript 满足两个条件时执行jquery函数
当满足两个条件时,我需要执行一个特定的函数Javascript 满足两个条件时执行jquery函数,javascript,jquery,jquery-callback,Javascript,Jquery,Jquery Callback,当满足两个条件时,我需要执行一个特定的函数mvFinishItUp()。更具体地说,一个条件是$回调成功。ajax另一个条件是正常的代码流,直到它到达函数为止。有点像这样: $.ajax({ url: mv_finalUrl, success: function (data) { mvFinishItUp(data); }, dataType: 'html' }); /* here a lot more code, with animations
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和函数本身之间的松散代码也应该转到函数…这里我添加了第二个参数来检查回调检查
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;
}
现在,
也许像这样的事情会起作用:
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调用调用的,它将不会执行。它跟踪是否已经用一个自包含的布尔值调用了它。这是一个完美的使用案例 从AJAX调用中删除
success:
参数,稍后注册处理程序:
var jqxhr = $.ajax(...);
// do some other synchronous stuff
...
// and *then* register the callback
jqxhr.done(mvFinishItUp);
延迟对象可以很好地(按设计)处理在AJAX事件完成后注册到该事件上的情况。试试下面的方法(这只是psuedo代码)
这是一段非常“难看”的代码,但您可以对其进行修改,使其不使用全局变量,因此这只是一个示例:
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时才执行。因此,没有谁先调用它,只有当两个标志设置为true时,才会执行它
一种更简洁的方法是在对象中实现函数,并使用属性而不是全局变量。但是,如果
$.ajax
回调在整个剩余代码中更快,我的mvFinishItUp将在剩余代码之前被调用(执行它的第二个标准),对吗?@Eduardo你说的其他代码是什么?这更容易,因为$.ajax
有一个回调。。但我们如何才能确定世界其他地区的终结呢code@Vega整个代码位于$.ajax
函数和最终的mvFinishItUp
函数之间。基本上,mvFinishItUp
只能在ajax和所有其他代码都完成时执行。我不认为你理解他的问题,或者我不理解。。阅读下面的评论post@Vega我相信我已经完全理解了这个问题。我很想知道如果jqxhr.done(mvFinishItUp)代码>在成功回调后执行。这意味着他的ajax代码比他的函数代码要快,成功回调是在它到达之前完成的。完成@alnitak在这里是正确的。。。在我看来,您的其他代码需要在自定义延迟函数中。通读jquery api中的$。when()
@Eduardo是的,完全正确。如果有,回调将立即开始执行。如果$.ajax
函数是第一个调用mvFinishItUp
之后的函数,当代码到达自身时,变量data
仍将被设置?否,但我们可以更改它。编辑我的答案。不,更干净的jQuery方法是使用延迟对象…;-)我同意,我刚刚读了你的答案,比我的好得多,所以,再投你一票!谢谢
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
}
}
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 */
}
}