ajax-返回并防止延迟.then()执行

ajax - return and prevent deferred.then() execution

本文关键字:then 执行 延迟 返回 ajax-      更新时间:2023-09-26

我有一个AJAX请求,如下所示:

$.ajax({
   type: 'GET',
   url: url,
   dataType: "json", 
   success: function(data) {
      // ...
      callbak(true);
   },
  })
  .then( 
    function( response ) {
        // ...
    });

我想运行那个回调函数,从而退出success函数中的ajax请求,并阻止执行deferred.then()。在我的情况下,callback被激发,但之后deferred.then()也被执行,我不希望这种情况发生。

知道吗?

感谢

使用这样的标志:

var executeThen = true;
$.ajax({
        type: 'GET',
        url: url,
        dataType: "json",
        success: function(data) {
            // ...
            executeThen = false;
            callbak(true);
        },
    })
    .then(function(response) {        
        if(executeThen){
         // ...
        }
    });