简体   繁体   English

jQuery Ajax:自动刷新

[英]Jquery Ajax : auto-refresh

In my Ajax success function i created button and on click i am calling a function. 在我的Ajax成功函数中,我创建了按钮,然后单击以调用函数。 The problem: The page reloads based on the timer in set interval but when i click the button it will call the function based on the number of times the page reloaded. 问题:页面在设置的时间间隔内基于计时器重新加载,但是当我单击按钮时,它将基于页面重新加载的次数调用该函数。

For example: If page reloads 5 times and then i call a function on clicking that button-it will call that function 5 times. 例如:如果页面重新加载5次,然后单击该按钮,则调用一个函数,它将调用该函数5次。 if it reloads 10 times then function call is for 10 times. 如果重新加载10次,则函数调用将重复10次。

Please advice what i am doing wrong? 请指教我在做什么错?

Here is the code: 这是代码:

        $(document).ready(
                function() {
               setInterval(function() {                  
                         $.ajax({
                  type: 'GET',
                   url: 'Refresh',
              success: function(data) {
                 var trHTML = '';
            $.each(data, function(i, item) {
    var buttonVar = ('<button id="bt21" class="btn121">' + "STOP" + '</button>');
          trHTML += '<tr><td>'+buttonVar+'</td></tr>'
            });
          $('#test1').append(trHTML);

          $(document).on('click','#bt21', function(event) {
         var rownum1 = $(this).closest('tr').index();
       stopTest(data[rownum1].process_id);
         });
             }
         });
      }, 5000);
  });

You have set the AJAX call to be made every 5 seconds. 您已将AJAX呼叫设置为每5秒进行一次。 Each time time this function is called, you are also attaching the click event on the button you append. 每次调用此函数时,您还将在您添加的按钮上附加click事件。 So there will be multiple event handlers attached to the same element. 因此,将有多个事件处理程序附加到同一元素。 You need to clear any existing event handlers on that element before you attach another if you want to stick to your current code. 如果要坚持当前代码,则需要先清除该元素上所有现有的事件处理程序,然后再附加另一个。 Here's how to do it: 方法如下:

$(document).off('click', '#bt21');
$(document).on('click','#bt21', function(event) {
     var rownum1 = $(this).closest('tr').index();
   stopTest(data[rownum1].process_id);
     });

First you are appending buttons on refresh that have the same id attribute so that's going to cause you issues at some point. 首先,您要在刷新上附加具有相同id属性的按钮,以便在某些时候引起问题。

What you need to do is move your click event outside of the interval function and ajax callback. 您需要做的是将click事件移到interval函数和ajax回调之外。 Add the process id to the button in a data attribute and delegate a click event to the button so it will work even though the elements haven't been created in the DOM when the page loads. 将流程ID添加到data属性中的按钮,并将click事件委托给该按钮,这样即使页面加载时尚未在DOM中创建元素,该事件也将起作用。

Here's an example although I'm not sure if it works (can't really simulate this easily): 这是一个示例,尽管我不确定它是否有效(无法真正轻松地模拟它):

$(document).ready(function() {

    setInterval(function() {        
        $.ajax({    
            type: 'GET',
            url: 'Refresh',
            success: function(data) {
                var trHTML = '';
                $.each(data, function(i, item) {
                    var buttonVar = '<button class="btn" data-process-id="' + item.process_id + '">' + "STOP" + '</button>');
                    trHTML += '<tr><td>'+buttonVar+'</td></tr>'
                });

                $('#test1').append(trHTML);
             }
         });
      }, 5000);

    $('#test1').on('click', '.btn', function() {
        stopTest( $(this).data('process_id') );
    });
});

Each time the page is refreshed from your ajax call a new event listener is bound to the button in memory. 每次从ajax调用刷新页面时,都会将新的事件侦听器绑定到内存中的按钮。 You need to clear the event listeners then create a new one. 您需要清除事件监听器,然后创建一个新的监听器。

$(some element).unbind().on(...........);

I always unbind event listeners created in an ajax call if anything to keep the browser memory from being over loaded or to prevent the issue you are having. 我总是取消ajax调用中创建的事件侦听器的绑定,以防止浏览器内存过载或防止出现问题。

$(document).ready(
            function() {
           setInterval(function() {                  
                     $.ajax({
              type: 'GET',
               url: 'Refresh',
          success: function(data) {
             var trHTML = '';
        $.each(data, function(i, item) {
var buttonVar = ('<button id="bt21" class="btn121">' + "STOP" + '</button>');
      trHTML += '<tr><td>'+buttonVar+'</td></tr>'
        });
      $('#test1').append(trHTML);

      $(document).unbind().on('click','#bt21', function(event) {

     var rownum1 = $(this).closest('tr').index();
   stopTest(data[rownum1].process_id);
     });
         }
     });
  }, 5000);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM