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

464
Views
setTimeout no funciona correctamente en el ciclo while

Necesito que se detenga durante 3 segundos al final de cada ciclo, pero no se detiene. ¿Cómo puedo solucionar esto? Los códigos de ejemplo serían apreciados.

 function file_get_contents(url, callback) { fetch(url).then(res => res.text()).then(text => callback(text)); } function theCallBack(text) { text = text.replace(/google/g, "") let matches = text.match(/www.[az\-]+?.com/g); console.log(matches[0]); }; var theArray = ["https://www.google.com/search?q=one", "https://www.google.com/search?q=two", "https://www.google.com/search?q=three"]; var count = theArray.length; while (count > 0) { console.log(count); count--; file_get_contents(theArray[count], theCallBack); setTimeout(function() {}, 3000); }

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede usar una Promise para pausar el programa y await .

 const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms)); function file_get_contents(url, callback) { fetch(url).then(res => res.text()).then(text => callback(text)); } function theCallBack(text) { text = text.replace(/google/g, "") let matches = text.match(/www.[az\-]+?.com/g); console.log(matches[0]); }; var theArray = ["https://www.google.com/search?q=one", "https://www.google.com/search?q=two", "https://www.google.com/search?q=three"]; var count = theArray.length; (async() => { for (let i = 0; i < count; i++) { console.log(i + 1); file_get_contents(theArray[i], theCallBack); await wait(3000); } })();

about 4 years ago · Juan Pablo Isaza Report

0

Creo que el problema es la función asíncrona setTineout(), por lo que debe sincronizarla para que funcione como desee.

https://developer.mozilla.org/ru/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals

https://stackoverflow.com/a/50357618

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!