如何打破setInterval启动另一个



我想在我的网站中自动重新加载div。如果我在使用clear Interval停止interval1时单击底部脚本重新加载另一个Div,则在使用console.log Interval时,它不再使用脚本重新加载 interval1,我会发现false,脚本再次重新加载clear Interval不起作用或

<script type="text/javascript">
  var interval2;
  var interval1;
  $(document).ready(function() {
    var get_template = function() {
      $("#live-grouped-odds-current").load("template.php");
    }
    interval2 = setInterval(get_template, 6000);
    $("#live_details").click(function() {
      clearInterval(interval2);
      console.log(interval2);
      $('#live-overview-menuitem').removeClass('Actual');
      $('#live_details').addClass('Actual');
      $("#button_sp").css('display', 'none');
      $("#button_dt").css('display', 'block');
      clearInterval(interval2);
      interval2 = false;
      $("#live-grouped-odds-current").empty();
      $("#loader12").css('visibility', 'visible');
      $("#live-grouped-odds-current").load("details.php");
    });
    $("#loader12").css('visibility', 'visible');
    $.get("details/events.php", function(data1) {
      $("#event_list").html(data1);
    });
    $("#loader12").css('visibility', 'hidden');
    var refresh = function() {
      $.get("details/events.php", function(data1) {
        $("#event_list").html(data1);
      });
      $.get("details/simple_event.php", function(data) {
        $("#simple_event").html(data);
      });
    }
    interval1 = setInterval(refresh, 6000);
  });
  function simple_event(id) {
    clearInterval(interval1);
    $('.Event').removeClass('Actual');
    $("#" + id).attr('class', 'Actual');
    $("#simple_event").empty();
    $("#loader12").css('visibility', 'visible');
    $.get("details/simple_event.php?id=" + id, function(data) {
      $("#simple_event").html(data).hide();
      $("#simple_event").slideDown(1000);
      //alert( "Load was performed." );
      $("#loader12").css('visibility', 'hidden');
    });
    interval1 = setInterval(refresh, 6000);
  }
</script>

在调用cleartimeout时,这意味着不会再次触发间隔,但是如果$("#live-grouped-odds-current").load("template.php");仍在运行,则将完成。如果$("#live-grouped-odds-current").load("template.php");花费的时间超过6000ms,则您也将开始并行执行多个请求。(我认为最好使用Settimeout)

首次致电ClearInterval(Interval2),将Intertal2设置为 undefined

$("#live_details").click(function() {
  clearInterval(interval2);
  inteval2 = undefined;
});

和更改 get_template to

var get_template = function() {
  $.get("template.php", function( data ) {
    if (inteval2 !== undefined) {
      $("#live-grouped-odds-current").html( data );
    }
  });
}

因此,如果已清除间隔

,则#live-grouped-odds-current将不会未注明日期

最新更新