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

120
Views
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 answers
Answer question

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