2011-12-08 21 views
0

当某个字段关注或模糊时,会触发jQuery Ajax请求。如果已经有一个请求被调用,我不希望Ajax请求触发。这里是我有什么:暂时禁用jQuery中的某些绑定操作

$('#domain').bind('change focus blur', function() { 
    // Disable the "bind" here 
    $.getJSON(theURL, '', function(data) { 
     ... 
    }); 
    // Re-enable the "bind" here 
}); 
+0

我创建了一个函数队列,你可以修改和再利用的另一个问题。它在http://jsfiddle.net/6tvTD/2/ – JesseBuesking

回答

1

unbind怎么样?

function handler() { 
    $('#domain').unbind('change focus blur', handler); 

    $.getJSON(theURL, '', function(data) { 
     // ... 

     $('#domain').bind('change focus blur', handler); 
    }); 
} 

$('#domain').bind('change focus blur', handler); 
+0

所以你必须在另一个bind()调用中包装handler()? – BZink

+0

@BZink:嗯......不。 – Ryan

0

设置一个变量超出绑定函数的范围,并使用它来跟踪请求是否正在进行。确保只有在json返回后才能重新启用。如果您重新启用了您的评论,那么请求仍可能正在进行中,但执行将继续超出getJson调用。

var blocking = false; 
    $('#domain').bind('change focus blur',function(){ 
     if(!blocking) 
     { 
      blocking = true; 
      $.getJSON(theURL,'',function(data){ 
       blocking = false; 
      }); 

      // Re-enable the "bind" here - no, not here. you should re-enable on the callback from the ajax call. 
     } 
    }); 
0
$('#domain').bind('change focus blur',function(){ 

    //check if blocked   
    if ($(this).data("blocked")) return; 

    // Disable the "bind" here 
    $(this).data("blocked", 1); 

    $.getJSON(theURL,'',function(data){ 

    }); 
    // Re-enable the "bind" here 
    $(this).data("blocked", 0); 
}); 
相关问题