0

在发布此内容之前,我已经阅读了大量内容,并尽我所能让它发挥作用。所以基本上我正在尝试使用 ajaxStop() 作为一种钩子来完成所有其他 jQuery ajax 请求之后的操作,这样我就可以对其进行操作和添加更多内容。

但似乎当我确实使用 ajaxStop() 时,它似乎多次执行我在请求中添加的附加内容......

现在经过一些试验和错误。我了解到这是因为在我的 ajaxStop() 函数中,一旦单击某些内容,我将调用另一个 ajax 帖子来获取数据,以填充弹出窗口。我需要在加载所有其他 ajax 后注册此单击事件处理程序...因为如果我将其从 ajaxStop() 方法中取出,多重性错误就会消失,但如果我通过 ajax 刷新内容而不是单击事件不会' t 注册了。我很可能会解决这个错误,但可以使用一些帮助。时间不早了,我的脑子很痛,所以明天我会重新审视它。

jQuery('.content').one('ajaxStop', function() {

 setInterval(doPopUpOptionsMenu , 1000);
 return false;

 });

doPopUpOptionsMenu 内部是另一个 ajax 调用,用于获取选项弹出的内容。

  function doPopUpOptionsMenu() {
 jQuery(".activity-popUpMenu").one('click',function(e) {
 var activityId = jQuery(this).parent().attr('id').split('-');
 //code to grab options and show the user the Options box.

   jQuery("#optionsPopUpContainer").css({'display': 'block', 'position' :'absolute',   'top' : e.pageY , 'left' : e.pageX});
var myAjaxURL = "example.com";
  var jqxhr = jQuery.post(myAjaxURL, { menuOptions : true, id : activityId[1] } ,     function($response) {
 jQuery("#optionsPopUpContainer").html($response);
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

 });
 jQuery(".activity_update").hover(
 function(){
 jQuery(this).children('.activity-popUpMenu').addClass('activated');

 } ,
function() {
 jQuery(this).children('.activity-popUpMenu').removeClass('activated');
 //jQuery(".activity-popUpMenu").unbind('click');
  });

 }

现在,第一次单击时会弹出两次警报,一次成功,一次完成。然后我再次单击弹出选项,它会弹出四个警报,而不是 8 个比 16 个,并不断增加一倍的弹出窗口。就像我说的那样,我想这是由于 ajaxStop 调用中的 ajax 造成的,但我怎样才能绕过它或做一些更好的事情......?

4

5 回答 5

1

我偶然发现这篇文章正在寻找答案,因为我遇到了类似的问题。对于我的情况,我.load()用来将各种文件加载到我的页面中。当我使用 Vic 的示例时$(document).ajaxComplete(function(){ //my function });,我的函数实际上最终运行了 5 次。我有 5 个 .load() 请求。所以在我看来,.ajaxComplete()每次请求后都会运行该函数。

对于我的解决方案,我使用...

$(document).ajaxStop(function () {
   //my function

});

它运行了一次函数,这就是我想要的。

于 2012-08-16T18:19:37.970 回答
0

如果您在队列中跟踪 AJAX 请求,您将能够检查在 $.ajaxStop 触发之前发出的实际请求。问题是您必须在 AJAX 调用上放置一个 doNotQueue 属性,这样您就可以将选定的调用排除在队列跟踪之外。

如果您在 $.ajaxSend 中使用 jqXHR 对象,这实际上并不太难。这是一些示例咖啡脚本代码,应该可以帮助您:

$.xhrPool = []
# When sent, all ajax requests get pushed into the XHR pool so we can abort/access
# them if necessary.
$(document).ajaxSend((event, xhr, ajaxOptions) ->
  unless ajaxOptions.doNotQueue
    $.xhrPool.push(xhr)
)

# Whenever an ajax call completes, remove it from the queue and then perform the
# desired operation if the queue is empty.
$(document).ajaxComplete((event, xhr, ajaxOptions) ->
  index = $.xhrPool.indexOf(xhr)

  # If this is a request being tracked by the queue, remove it from the queue
  if index > -1
    $.xhrPool.splice(index, 1)

    # If the queue is now empty then all ongoing AJAX calls we want to track 
    # have finished.
    if $.xhrPool.length == 0
      # Perform your desired action here, but make sure that if you make an AJAX
      # call you include the doNotQueue property. Note that this will fire every
      # time your queue runs out, so if you want to do something only once globally,
      # you'll have to keep track of that yourself.
)
于 2012-06-06T18:03:33.333 回答
0

确保该async属性未设置为false.

...我一直忽略的东西。

于 2014-11-12T10:38:33.860 回答
0

如果您想在文档中的所有 ajax 调用完成后使用某些东西,请使用 $(document).ajaxComplete(function(){handler}); 这可能不是最好的方法,但如果你想听所有 ajax 完成,它确实工作得很好。

于 2012-05-04T08:25:22.317 回答
0

我有一个类似的问题。我所有的 ajax 请求都执行了 ajaxStop 中的代码。我在页面加载时有一些 ajax 请求来构建内容,这些应该是唯一使用 ajaxStop 调用的请求。我找到的解决方案是取消绑定 ajaxStop 函数,使其只运行一次。

$(document).ajaxStop(function() {
    customFunction();       
    $(document).unbind("ajaxStop");
});
于 2020-01-15T17:44:51.777 回答