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

123
Views
cómo hacer una llamada ajax recursivamente, para verificar si el servicio está disponible

Estoy tratando de hacer una llamada ajax de forma recursiva para verificar si el servicio web está disponible o no. Aquí está mi código

 function checkWebService() { console.log("function called") $.ajax({ url: "localhost:8085/login/checkWebServiceIsRunning", type: 'GET', success: (response) => { console.log("success") }, error: (jqXHR, textStatus, errorThrown) => { console.log("error") setTimeout(() => { checkWebService() }, 1000) } }) }

Como mi servicio no se está ejecutando inicialmente, el código del bloque de error se ejecuta, pero después de dos o tres veces, la llamada ajax se atasca, no funciona correctamente ni falla.

Luego intenté poner el tiempo de espera en el código ajax, por lo que dio un error de tiempo de espera, pero incluso si el servicio web está disponible y el código del lado del servidor se ejecuta sin demora, ajax genera un problema de tiempo de espera.

¿Puede alguien ayudar en lo que podría estar yendo mal aquí?

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

0

Puede usar un ciclo while y async/await para obtener la respuesta.

Además, puede agregar un recuento variable para attempt evitar un bucle infinito.

Además, se agrega una función de wait para retrasar cada llamada para no utilizar el programa.

Nota: errorCode:789 es solo un ejemplo para detectar el error de intento máximo. puedes definir el tuyo propio.

 const wait = ms => new Promise((resolve, reject) => setTimeout(resolve, ms)); async function checkWebService() { const MAX_ATTEMPT = 20; let attempt = 0; while (true) { try { attempt++; if (attempt > MAX_ATTEMPT) throw { errorCode: "789" }; const response = await $.ajax({ url: "localhost:8085/login/checkWebServiceIsRunning", type: "GET", }); return response; } catch (err) { if (err.errorCode === "789") { throw new Error("Exceed Max. attempt."); } console.log(err); await wait(1000); continue; } } } checkWebService().then(console.log).catch(console.log);
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!