Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda