Stop implementation setInterval



  • $(setInterval (function() { 
        if ( $("#online_counter").html() == 1) {
            $("wrapper").hide();
        }
        if ( $("#online_counter").html() == 2) {
            window.location.replace(document.location.href);
            $("wrapper").show();
        }
    }, 3000 ));
    

    Distinguished, can jQuery stop performance setIntervalwhen condition 2 is fulfilled?



  • Second condition bodyif ( $("#online_counter").html() == 2) { Reloads the page, which already suggests a bad code and approach. Set a variable ID interval and clean it up:

    $(function() {
      let id = setInterval(function() { // setInterval возвращает уникальный ID вида: 3681
        if ($("#online_counter").html() == 1) {
          $("wrapper").hide();
        }
        if ($("#online_counter").html() == 2) {
          clearInterval(id); // Функция уничтожения интервала по ID
          $("wrapper").show();
        }
      }, 3000)
    });
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2