Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda