【发布时间】:2021-10-30 07:19:04
【问题描述】:
我需要它在每个循环结束时暂停 3 秒,但它没有暂停。我该如何解决?示例代码将不胜感激。
function file_get_contents(url, callback) {
fetch(url).then(res => res.text()).then(text => callback(text));
}
function theCallBack(text) {
text = text.replace(/google/g, "")
let matches = text.match(/www.[a-z\-]+?.com/g);
console.log(matches[0]);
};
var theArray = ["https://www.google.com/search?q=one", "https://www.google.com/search?q=two", "https://www.google.com/search?q=three"];
var count = theArray.length;
while (count > 0) {
console.log(count);
count--;
file_get_contents(theArray[count], theCallBack);
setTimeout(function() {}, 3000);
}
【问题讨论】:
-
你需要暂停什么?
-
while循环不等待setTimeouts -
3 seconds at the end of every loop,你的意思是3 seconds at the end of every successful fetch()吗?
标签: javascript