Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Cómo ejecutar una función asíncrona en js con tiempos de espera (para limitar la velocidad)

Estoy diseñando un sistema en el que un usuario interactuará con mi API RESTful a través de JS y reemplazará el elemento ts html con un SVG diferente si la respuesta devuelta es true . Si la llamada a la API devuelve false , el JS debe esperar 5 segundos antes de volver a intentar la llamada a la API. de nuevo, si la llamada a la API devuelve true , se cambiará el SVG y la función dejará de llamar a la API. ¿Cómo puedo hacer que la función "duerma" durante 5 segundos antes de volver a ejecutarla y finalizar la función si la llamada API devuelta es true ?

Código:

 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 answers
Answer question

0

No use el modo de suspensión ya que pausa su programa para pausar. En su lugar, use setTimeout así. Esto crea un detector de eventos que vuelve a llamar a la función después de 5 segundos si tStat es false .

En caso de false , la función establecerá un tiempo de espera después del cual se volverá a llamar a la función. Después de que se completa el tiempo de espera, se borra solo. Este paso se repite hasta que la solicitud de API finalmente se resuelve como 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!