How can I stop a setInterval to run after executing a AJAX request that replaces that setInterval in the DOM with another setInterval?

五迷三道 提交于 2019-12-11 20:19:46

问题


I am using jQuery and I have some trouble on stopping to work the setInterval when performing a AJAX request that returns the same code containing the same setInterval. That is:

Given I have the following code:

<div id="test_css_id">
  <a id="link_css_id" href="test_url.html">LINK</a>

  <script type="text/javascript">
    $('#link_css_id').click(function(event) {
      event.preventDefault();

      $.ajax({
        url:     $(this).attr('href'),
        type:    'PUT',
        success: function(data) {
          $('#test_css_id').replaceWith(data); // Replaces all code including JavaScript with the response data (note: the response data is exactly the same as the code shown here).
        }
      });
    });

    $(document).ready(function() {
      var refreshTimer;

      function refreshFunction(){
        $.ajax({
          url:     'test_url.html',
          type:    'GET',
          success: function(data) {
            $('#test_css_id').replaceWith(data); // Replaces all code including JavaScript with the response data (note: the response data is exactly the same as the code shown here).
          },
          complete: function(data) {
            clearInterval(refreshTimer); // Note: This'd not stop the setInterval.
          }
        });
      }

      refreshTimer = setInterval(refreshFunction, 1000); // milliseconds
    });
  </script>
</div>

When the AJAX request within the click function runs on success then the above code is reloaded (note: the rendered data in the replaceWith is exactly the same as the above code, including JavaScript). However the setInterval is not "overrode" / "stopped" and so the browser runs one more time that setInterval for each time I clicked theLINK. The same does not happen when the refreshFunction runs. However, depending on the amount of previous click on the LINK, even the refreshFunction causes the setInterval to run more and more.

How can I stop the setInterval to run when the AJAX request success so to have only one setInterval running?


回答1:


You will need to clear the timer right before you perform the replacement. For this, you'll need the timer variable to be accessible within the click callback as well. In this case, I have made the timer global, but there are other ways you can do it, I'll leave that up to you.

<div id="test_css_id">
  <a id="link_css_id" href="test_url.html">LINK</a>

  <script type="text/javascript">
    var refreshTimer;  //******************** Timer is now global
    $('#link_css_id').click(function(event) {
      event.preventDefault();

      $.ajax({
        url:     $(this).attr('href'),
        type:    'PUT',
        success: function(data) {
          //************* Clear interval if it exists before replacing code.
          clearInterval(refreshTimer);
          $('#test_css_id').replaceWith(data); // Replaces all code including JavaScript with the response data (note: the response data is exactly the same as the code shown here).
        }
      });
    });

    $(document).ready(function() {
      function refreshFunction(){
        $.ajax({
          url:     'test_url.html',
          type:    'GET',
          success: function(data) {
            $('#test_css_id').replaceWith(data); // Replaces all code including JavaScript with the response data (note: the response data is exactly the same as the code shown here).
          },
          complete: function(data) {
            clearInterval(refreshTimer); // Note: This'd not stop the setInterval.
          }
        });
      }

      refreshTimer = setInterval(refreshFunction, 1000); // milliseconds
    });
  </script>
</div>


来源:https://stackoverflow.com/questions/18898676/how-can-i-stop-a-setinterval-to-run-after-executing-a-ajax-request-that-replaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!