【问题标题】:Stopping setInterval in javascript [duplicate]在javascript中停止setInterval [重复]
【发布时间】:2014-09-05 16:46:28
【问题描述】:

我有一个模拟热图的按钮。我在点击按钮时使用 setInterval 函数

 <button onclick="setInterval(function(){update_map()},50);">simulate</button>

我的功能如下:

function update_map(){
  counter+=50;
  var i =1
  for(i=counter-50;i<<?php echo json_encode($lat); ?>.length & i<counter;i++){
  taxiData.push(new google.maps.LatLng(<?php echo json_encode($lat); ?>[i],<?php echo json_encode($log); ?>[i]));
 }
  var pointArray = new google.maps.MVCArray(taxiData);
heatmap = new google.maps.visualization.HeatmapLayer({
    data: pointArray
  });

  heatmap.setMap(map);
  if(i==<?php echo json_encode($lat); ?>.length )
  clearInterval(setInterval(function(){update_map()},50));
}

我们将不胜感激任何帮助或建议。

【问题讨论】:

  • 检查docssetInterval 返回一个 ID 号,可用于后续对 clearInterval 的调用。

标签: javascript button setinterval


【解决方案1】:

很简单。将间隔设置为变量,然后将其清除。

var refreshIntervalId = setInterval(fname, 10000);

/* later */
clearInterval(refreshIntervalId);

Stop setInterval call in JavaScript

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 2010-09-11
    相关资源
    最近更新 更多