setTimeout within setInterval

Asked

Viewed 76 times

0

There is a way to do this on Node.js?

setInterval (every 5 seconds)

Some functions (about 3 - 4 seconds)

setTimeout (3 seconds)

End

So every 5 seconds run a function I don’t know how long it will spend as I do it?

Ex:

setInterval(() => {

        this.tradeApi.getAccountInfo((acountInfo) => {
            if (acountInfo.balance.brl.available > this.config.invest) {
                console.log(chalk.yellow("Aguardando saldo para operar..."));
            } else {
                this.infoApi.ticker((ticker) => {
                    console.log(chalk.cyan("O valor da moeda esta: ") + chalk.green("R$ " + Number(ticker.ticker.last).toFixed(5)));
                    console.log(chalk.cyan("Estou colocando uma ordem de compra a: ") + chalk.green("R$ " + Number(ticker.ticker.last / this.config.decay).toFixed(5)) + chalk.cyan(" por " + process.env.CRAWLER_INTERVAL + " segundos e ver se vende..."));

                    setTimeout(() => {
                        console.log("esperei 5 s");
                    }, process.env.CRAWLER_INTERVAL)

                })
            }
        })


    }, process.env.CRAWLER_INTERVAL)
  • 1

    Your question is unclear. Could you explain it a little better? Have you ever elaborated some code trying to implement this?

  • Instead of doing it this way, why doesn’t it run the task, wait X minutes and run again? No matter how long it takes. For example: loop infinito { executa_tarefa(); esperarMinutos(5) }

No answers

Browser other questions tagged

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