2011-10-22 4 views
1

가 완료 될 때까지 이전 이벤트의 실행을 방지하는 방법 : 목록에 새 항목을 삽입하고 마지막 하나를 제거마지막 호출이 나는이 jQuery를 이벤트가

$('#button-next').bind('click', function() { 
    $.ajax({ 
     type: "GET", 
     dataType: "json", 
     url: 'index.php?route=product/category/display&cat=any&limit='+p+',1', 
     cache: false, 
     success: function(data) { 
      var text = data.output; 
      goForward('scroller', text); 
      p++; 
     } 
    }); 
}); 

합니다. 문제는 사용자가 버튼을 너무 빨리 클릭하기 시작하면 엉망이되는 것입니다. 마지막 통화가 끝나기 전에 새로운 통화를 막을 수있는 방법이 필요합니다.

function process() { 
    $.ajax({ 
     type: 'GET', 
     dataType: 'json', 
     url: 'index.php', 
     cache: false, 
     data: { 
      route: 'product/category/display', 
      cat: 'any', 
      limit: p + ',1' 
     }, 
     success: function(data) { 
      var text = data.output; 
      goForward('scroller', text); 
      p++; 
      // we rebind the click event once again 
      $('#button-next').one('click', process); 
     } 
    }); 
} 

답변

2

당신은 클릭 동작에 가입하기 위해 .one() 방법을 사용할 수 있습니다 아약스 완료

  $('#button-next').bind('click', function() { 
       $('#button-next').attr('disabled', 'disabled'); 
       $.ajax({ 
        type: "GET", 
        dataType: "json", 
        url: 'index.php?route=product/category/display&cat=any&limit='+p+',1', 
        cache: false, 

        success: function(data) { 
          $('#button-next').removeAttr('disabled') 
          var text = data.output; 
          goForward('scroller', text); 
          p++; 
        } 
       }); 
      }); 
+2

대신에 '$ ('# button-next '). clic k (function()'$ ('# button-next')) 하나 ('click', function()'. 이것은 unbind를 불필요하게 만들고 한 줄을 절약합니다. – topek

+1

@topek, 훌륭한 아이디어. 귀하의 의견을 고려하여 내 게시물을 업데이트했습니다. 이것을 지적 해 주셔서 감사합니다. –

1

가 클릭의 버튼을 비활성화 한 후 활성화 :

$('#button-next').one('click', process); 

이 같은 process 함수를 정의 할 수있는 곳 :

관련 문제