2011-12-08 20 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); 
}); 
相關問題