Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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 Chrome中重复获取URL的代码片段不';行不通_Javascript_Google Chrome_Fetch Api - Fatal编程技术网

Javascript Chrome中重复获取URL的代码片段不';行不通

Javascript Chrome中重复获取URL的代码片段不';行不通,javascript,google-chrome,fetch-api,Javascript,Google Chrome,Fetch Api,我可以在Chrome控制台中使用此代码向我的localhost上的URL发出post请求: fetch("http://localhost:50014/api/myapi/", {"credentials":"omit","headers":{"accept":"*/*","accept-language":"en-GB,en-US;q=0.9,en;q=0.8","cache-control":"no-cache","content-type":"application/json","prag

我可以在Chrome控制台中使用此代码向我的
localhost
上的URL发出post请求:

fetch("http://localhost:50014/api/myapi/", {"credentials":"omit","headers":{"accept":"*/*","accept-language":"en-GB,en-US;q=0.9,en;q=0.8","cache-control":"no-cache","content-type":"application/json","pragma":"no-cache","sec-fetch-mode":"cors","sec-fetch-site":"cross-site","x-api-key":"abc123"},"referrerPolicy":"no-referrer","body":"{\"nodeIds\":[\"ABC123\"]}","method":"POST","mode":"cors"});
我将此代码用作开发人员工具中的Chrome“代码片段”,尝试重复发出post请求,用于某些测试目的,其中我尝试识别错误:

while(1==1) {
fetch("http://localhost:50014/api/myapi/", {"credentials":"omit","headers":{"accept":"*/*","accept-language":"en-GB,en-US;q=0.9,en;q=0.8","cache-control":"no-cache","content-type":"application/json","pragma":"no-cache","sec-fetch-mode":"cors","sec-fetch-site":"cross-site","x-api-key":"abc123"},"referrerPolicy":"no-referrer","body":"{\"nodeIds\":[\"ABC123\"]}","method":"POST","mode":"cors"});
setTimeout(() => {  console.log("waiting"); }, 1000);
}
但我从未看到POST请求生效,控制台中也没有任何内容。为什么?

更新

谢谢@Jaromanda X。我更新了这个,但它仍然没有运行。为什么?

while(1==1){
  callApi();
  sleep(5000);
}

function callApi() {
    fetch("http://localhost:50014/api/myapi/", {"credentials":"omit","headers":{"accept":"*/*","accept-language":"en-GB,en-US;q=0.9,en;q=0.8","cache-control":"no-cache","content-type":"application/json","pragma":"no-cache","sec-fetch-mode":"cors","sec-fetch-site":"cross-site","x-api-key":"abc123"},"referrerPolicy":"no-referrer","body":"{\"nodeIds\":[\"ABC123\"]}","method":"POST","mode":"cors"});
}

function sleep(milliseconds) {
   var currentTime = new Date().getTime();

   while (currentTime + milliseconds >= new Date().getTime()) {
   }
}

如果您想在一段时间后重复发出请求,可以使用setInterval,如下所示:

setInterval(()=>{

fetch("http://localhost:50014/api/myapi/", {"credentials":"omit","headers":{"accept":"*/*","accept-language":"en-GB,en-US;q=0.9,en;q=0.8","cache-control":"no-cache","content-type":"application/json","pragma":"no-cache","sec-fetch-mode":"cors","sec-fetch-site":"cross-site","x-api-key":"abc123"},"referrerPolicy":"no-referrer","body":"{\"nodeIds\":[\"ABC123\"]}","method":"POST","mode":"cors"});

},5000)

您的while循环在一个紧密的循环中发送数千个请求-没有等待,因为setTimeout不是这样工作的-最终,您的浏览器应该警告您,有javascript导致页面无响应-如果没有,请使用现代浏览器