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

126
Vistas
how to do ajax call recursively, to check if service is available

I am trying to do ajax call recursively to check whether web service is available or not. Here is my code

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)
                }
            })

        }

As my service is not running initially, the code from error block gets executed but after two or three times, the ajax call gets stuck, it neither goes in success nor in error.

Then I tried putting timeout in ajax code, so it gave timeout error, but even if the web service is available and server side code gets executed without any delay, ajax gives timeout issue.

Can someone help on what could be going wrong here.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use a while loop and async/await to get the response.

Also, you can add a variable count for the attempt to prevent infinite loop.

Furthermore, a wait function is added to delay each call to not utilize the program.

Note: errorCode:789 is just an example to catch the max attempt error. you can define your own.

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 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