Perform three functions in the same setInterval

Asked

Viewed 141 times

0

How can I perform three functions within the same setInterval, and for one the time to update should be different.

I tried the following:

function sleep(ms) {
  return new Promise(resolve => setInterval(resolve, ms));
}
async function update() {
  for($i=0;$i<300;$i++){ // a cada 300x (5min) faz um refresh
      atualizaLeilao();
      atualizaLeilaoCancelado();
      updateClock();
      await sleep(1000);
  }
 location.reload(); 
}

$(document).ready(function () {
    update();
});

I need that function atualizaLeilaoCancelado(); is updated every 5 minutes, not every second.

  • Wendler, sorry for my comment without knowing your application as a whole, but isn’t there a logic flaw there? Unless you put a setInterval inside the other all will run at the same time, so this code snippet would be simpler to create 3 setIntervals there.

1 answer

1


To execute the atualizaLeilaoCancelado() every 5 minutes the simplest is to individualize this call in a setInterval separately with the respective range value:

function updateLeilaoCancelado(){
    setInterval(function(){
        atualizaLeilaoCancelado();
    }, 300000);
}

And then call them both in the main code:

$(document).ready(function () {
    update();
    updateLeilaoCancelado();
});

I would also advise you to revise a little it is logical because in update after 300 iterations of for, that are 5 minutes makes refresh to the page, so refresh every 5 minutes loses the sense.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.