2012-09-20 68 views
1

我有一系列按钮和foreach按钮的我有一个单独的。点击(函数(){取消的setInterval为按钮点击AJAX

$("#approved").button().click(function() { 
$("#waiting_approval").button().click(function() { 
$("#department_waiting_approval").button().click(function() { 
$("#department").button().click(function() { 

我有对的setInterval AJAX该刷新在该div中的数据 我面临的问题是,一旦用户点击不同的按钮,我想停止当前按钮单击阿贾克斯呼叫,并开始新的一个

希望这是有道理的,在此先感谢。

$("#approved").button().click(function() { 

setInterval(function(){ 
    $.ajax({ 
    type: "GET", 
    url: "holidays/academic_days.php?userid='+$('#approved_list').attr('class')", 
    dataType: "html", 
    success: function(data) { 
    jQuery("#approved_list").fadeOut(10 , function() { 
     jQuery(this).html(data); 
     $('#approved_list').show(); 
    $('#waiting_approval_list').hide(); 
      $('#department_waiting_approval_list').hide(); 
      $('#department_logs').hide(); 
    }).fadeIn(100); 

    } 
}) 
}, 100); 

}); 
+1

http://stackoverflow.com/questions/446594/abort-ajax-requests-using-jquery –

回答

1

这可能是有帮助的:

var thisInterval; 
$("#approved").button().click(function() { 

    thisInterval=setInterval(function(){ 
     $.ajax({ 
     type: "GET", 
     url: "holidays/academic_days.php?userid='+$('#approved_list').attr('class')", 
     dataType: "html", 
     success: function(data) { 
     jQuery("#approved_list").fadeOut(10 , function() { 
      jQuery(this).html(data); 
      $('#approved_list').show(); 
     $('#waiting_approval_list').hide(); 
       $('#department_waiting_approval_list').hide(); 
       $('#department_logs').hide(); 
     }).fadeIn(100); 

     } 
    }) 
    }, 100); 

    }); 

制作取消像这样的按钮。

$("#cancel").button().click(function() {clearInterval(thisInterval)} 
+0

由于预期不太工作。你知道我如何在ajax成功数据的某个部分setInterval,比如(#somediv)。所以按钮会加载整个事件,但是然后setInterval在某个div上,它会继续加载该div的间隔,直到单击另一个按钮。 – Codded

+0

我们是不是在做同样的事情?它会继续加载该div的间隔,直到单击另一个按钮(#cancel)。 – vusan

+0

我的意思是按钮会加载#approved_list,然后做ajax调用。在#somediv的调用setinterval中嵌套在原始的#approved_list中。因此,它会在按钮单击时加载#approved_list,并且只按间隔刷新#somediv – Codded

1

setInterval函数返回稍后可用于清除使用clearInterval例如该间隔的时间间隔的对象的处理程序:

var handler = setInterval(function(){ 
     console.log('interval'); 
    },1000); 

    setTimeout(function(){ 
     clearInterval(handler); 
    },5000);