Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
How to run an async function in js with timeouts (for rate limiting)

I'm designing a system where a user will interact with my RESTful API through JS, and will replace the ts html element with a different SVG if the returned answer is true. If the API call returns false, the JS should wait for 5 seconds, before retrying the API call. again, if the API call returns true, the SVG will be changed, and the function will stop calling the API. How can I have the function "sleep" for 5 seconds before running again, and ending the function if the returned API call is true?

Code:

async function getTransaction(){
var lookup = $("meta[name='lookup']").attr("content");  
axios.get('http://127.0.0.1:5000/client_api/transaction_status/' + lookup) 
.then(function (response) {
    var tStat = response.data.transactionStatus;
    console
    if(tStat){
      document.getElementById("ts").innerHTML = 'drawing the new svg and replacing the old one'
      console.log('transaction_found')
    }
    else if(!tStat){
      console.log("transaction not found")
    }
    else{
      console.log("error")
    }
  });
  }console.log("hello"); while(true){setTimeout(getTransaction,5000);}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Don't use sleep sice it pauses your programm to pause. Instead use setTimeout like so. This creates an event listener which calls the function again after 5s if tStat is false.

In case of false the function will set an timeout after which the function is called again. After the timeout is completed it clears itself. This step repeats until the api request eventually resolves to true

async function getTransaction() {
  var lookup = $("meta[name='lookup']").attr("content");

  axios.get('http://127.0.0.1:5000/client_api/transaction_status/' + lookup)
    .then(function(response) {
      var tStat = response.data.transactionStatus;
      console
      if (tStat) {
        document.getElementById("ts").innerHTML = 'drawing the new svg and replacing the old one'
        console.log('transaction_found')
      } else {
        console.log("transaction not found")
        setTimeout(getTransaction, 5000);
      }
    });
}
console.log("hello");

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda