Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 下次调用该函数时退出Java脚本中的循环_Javascript_Html_Node.js_Loops - Fatal编程技术网

Javascript 下次调用该函数时退出Java脚本中的循环

Javascript 下次调用该函数时退出Java脚本中的循环,javascript,html,node.js,loops,Javascript,Html,Node.js,Loops,我正在创建仪表板(图)。这将每3秒更新一次。 下面是我的代码的示例html按钮 实际上,当点击一个按钮,它将正常工作。它将每3秒更新一次。但当我单击next按钮时,第一个请求的循环不会停止。两个请求都将运行(两个循环同时运行)。我在html中看到这两个图形。这是错误的。 单击“下一步”按钮或“下一个请求”时,我希望退出上一个正在运行的循环。我只需要当前请求的输出。我怎样才能做到这一点 要取消已经重复的setInterval,可以使用clearInterval 请在此处阅读更多信息要取消已重复的s

我正在创建仪表板(图)。这将每3秒更新一次。 下面是我的代码的示例html按钮

实际上,当点击一个按钮,它将正常工作。它将每3秒更新一次。但当我单击next按钮时,第一个请求的循环不会停止。两个请求都将运行(两个循环同时运行)。我在html中看到这两个图形。这是错误的。 单击“下一步”按钮或“下一个请求”时,我希望退出上一个正在运行的循环。我只需要当前请求的输出。我怎样才能做到这一点


要取消已经重复的
setInterval
,可以使用
clearInterval


请在此处阅读更多信息

要取消已重复的
setInterval
您可以使用
clearInterval


在此处阅读更多信息

您可以使用clearInterval停止现有的setInterval。我认为下面的代码应该适合您

var runningInterval = null;
async function getData(per) { 
  var type = document.form1.types.value;
  if(runningInterval != null)
      clearInterval(runningInterval);
  runningInterval = setInterval(async function(){
           let data =  await fetch(`http://localhost:3001/api?type=${per}&state=${state}`);
           json = await data.json(); 
           await formatting(json, per, type);
        }, 3000);      
}

您可以使用clearInterval停止现有的setInterval。我认为下面的代码应该适合您

var runningInterval = null;
async function getData(per) { 
  var type = document.form1.types.value;
  if(runningInterval != null)
      clearInterval(runningInterval);
  runningInterval = setInterval(async function(){
           let data =  await fetch(`http://localhost:3001/api?type=${per}&state=${state}`);
           json = await data.json(); 
           await formatting(json, per, type);
        }, 3000);      
}
var runningInterval = null;
async function getData(per) { 
  var type = document.form1.types.value;
  if(runningInterval != null)
      clearInterval(runningInterval);
  runningInterval = setInterval(async function(){
           let data =  await fetch(`http://localhost:3001/api?type=${per}&state=${state}`);
           json = await data.json(); 
           await formatting(json, per, type);
        }, 3000);      
}