admin管理员组文章数量:1400078
I'm trying to add a delay inside a loop in Node.js. I have an array and need to call a function for each element of the array. The catch is that every such function call should have a gap of 30 seconds. Here's what I have tried -
const cprRedshift = async (page) => {
let query = "select links from schema.table", links = [], ranks = []
let data = await redshiftSelect(query)
data.rows.forEach(element => {
links.push(element.links)
})
let hostnames = await getDomainNames(links)
// one way
for(let i = 0; i < hostnames.length; i++){
await setTimeout(async () => await checkPageRank(hostnames[i]), 30000)
}
// another way
let i = 0
while(i < hostnames.length){
await checkPageRank(page, hostnames[i])
setInterval(() => ++i, 30000)
}
}
checkPageRank
is a function in the same script and I need to call it for all elements in the hostnames[] array, while keeping a gap of 30 seconds between each call. Any idea on how to achieve this would be appreciated. Thanks!
I'm trying to add a delay inside a loop in Node.js. I have an array and need to call a function for each element of the array. The catch is that every such function call should have a gap of 30 seconds. Here's what I have tried -
const cprRedshift = async (page) => {
let query = "select links from schema.table", links = [], ranks = []
let data = await redshiftSelect(query)
data.rows.forEach(element => {
links.push(element.links)
})
let hostnames = await getDomainNames(links)
// one way
for(let i = 0; i < hostnames.length; i++){
await setTimeout(async () => await checkPageRank(hostnames[i]), 30000)
}
// another way
let i = 0
while(i < hostnames.length){
await checkPageRank(page, hostnames[i])
setInterval(() => ++i, 30000)
}
}
checkPageRank
is a function in the same script and I need to call it for all elements in the hostnames[] array, while keeping a gap of 30 seconds between each call. Any idea on how to achieve this would be appreciated. Thanks!
3 Answers
Reset to default 4Here's a simplified example of a mon pattern for doing this sort of thing:
const hostnames = ["one", "two", "three", "four", "five", "six"];
function go (index = 0) {
// do whatever you need to do for the current index.
console.log(hostnames[index]);
// if we haven't reached the end set a timeout
// to call this function again with the next index.
if (hostnames.length > index + 1) {
setTimeout(() => go(index + 1), 1000);
}
}
// kick it off
go();
A variation of my previous answer could include passing and consuming the array itself instead of incrementing a counter:
const hostnames = ["one", "two", "three", "four", "five", "six"];
function go ([current, ...remaining]) {
// do whatever you need to do for the current item.
console.log(current);
// if there are items remaining, set a timeout to
// call this function again with the remaining items
if (remaining.length) {
setTimeout(() => go(remaining), 1000);
}
}
// kick it off
go(hostnames);
You can use something like
let aWait=(x)=>new Promise((resolve)=>setTimeout(resolve,x));
Then rewrite your loop to something like
for(let i = 0; i < hostnames.length; i++){
await checkPageRank(hostnames[i]);
await aWait(30000);
}
本文标签: nodejsHow can i make a waitFor(delay) function in javascript inside a loopStack Overflow
版权声明:本文标题:node.js - How can i make a waitFor(delay) function in javascript inside a loop? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744257210a2597538.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论