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!

Share Improve this question asked May 24, 2019 at 3:55 RohitRohit 1,5433 gold badges18 silver badges22 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Here'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