2

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 和函数本身之间的松散代码也应该转到函数......

4

5 回答 5

6

这是jQuery Deferred 对象的完美用例。

success:从 AJAX 调用中移除参数,稍后注册处理程序:

var jqxhr = $.ajax(...); 

// do some other synchronous stuff
...

// and *then* register the callback
jqxhr.done(mvFinishItUp);

延迟对象(通过设计)可以很好地应对在 AJAX事件已经完成后注册的事件。

于 2012-06-01T15:02:59.907 回答
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
 },

mvFinishItUp()在 ajax 完成以及 ajax 和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
    }
 }
于 2012-06-01T14:54:21.777 回答
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 

当代码流到达该点时,我使用了一个自调用函数执行,但如果它是从 AJAX 调用中调用的,它将不会执行。它跟踪它是否已经被一个自包含的布尔值调用。

于 2012-06-01T14:59:19.940 回答
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
   }
}
于 2012-06-01T15:07:51.957 回答
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 时才执行。所以没有谁先调用它,它只有在两个标志设置为 true 时才会执行。

一种更简洁的方法是在对象中实现函数,并使用属性而不是全局变量。

于 2012-06-01T15:18:35.210 回答