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

123
Vistas
Ending setInterval when ajax call is complete
    $.ajax({
                     
        url: vars.url,          
        type: "post",          
        data: r,    
        async: true,        
        processData: vars.process,
        contentType: vars.contenttype,
        
        beforeSend: function(){

            if(vars.loadbar == 'true'){

                setInterval(function () {

                    $.getJSON(domain + '/core/files/results.json',  function (data) {

                        console.log(data);  

                    })
                
                }, 1000);
            
            }

        },
        
        complete: function(){

            clearInterval();

        },

        succes: function(data){
            ..................
        }

    })   

So I am trying to end the infinite loop my code is spawning as soon as my ajax call is being done. It now phones pretty much every second to my file to get results, which i want to stop as soon as my ajax call is completed.

I am not sure how to approach this, since if i assign a variable to the setInterval (being it in the function of the beforeSend itself, or outside of the AJAX call), it either wont see the variable, or my variable is empty. I think I am approaching this wrong. Can I check within the beforeSend if the AJAX call is complete to end the loop?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

you can store your interval as a global variable and clear it when you need it. like so:

let interval;
   $.ajax({
        url: vars.url,          
        type: "post",          
        data: r,    
        async: true,        
        processData: vars.process,
        contentType: vars.contenttype,
        
        beforeSend: function(){

            if(vars.loadbar == 'true'){

                interval = setInterval(function () {

                    $.getJSON(domain + '/core/files/results.json',  function (data) {

                        console.log(data);  

                    })
                
                }, 1000);
            
            }

        },
        
        complete: function(){

            clearInterval(interval);

        },

        succes: function(data){
            ..................
        }

    }
over 4 years ago · Santiago Trujillo 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