www.webdeveloper.com
Results 1 to 5 of 5

Thread: Ajax / Stop setInterval Once Success

Hybrid View

  1. #1
    Join Date
    Nov 2006
    Posts
    9

    Question Ajax / Stop setInterval Once Success

    Hello,

    I have some ajax code to get the some progress indications from a json file. Everything is working well, however the setInterval never ends.

    I was hoping that once the $.post success event had been fired that it would of stop the setInterval part by setting varLoading to false.

    Code:
    $.post('work.php', function(data){
    	alert("finished");
    	varLoading = false;
    });
    
    if(varLoading) {
    	setInterval(function() {
    	    $.getJSON('progress.json', function(data) {
    	    	progress(data.progress, data.message);
    	    });
    	}, 2000);
    }
    I then tried adding it into a function, and calling that based on the status of the varLoading variable being true or false. However this seems to increment alot quicker than 2 seconds and caused by browser to crash. I think this is also going on into 'infinity and beyond' :

    Code:
    $.post('work.php', function(data){
        alert("finished");
        varLoading = false;
    });
    
    if(varLoading){
        checkStatus();
    }
    
    function checkStatus() {
        if(varLoading) {
            setInterval(function() {
                $.getJSON('progress.json', function(data) {
                    progress(data.progress, data.message);
                });
                checkStatus();
            }, 2000);
        }
    }
    Could anyone help point me in the right direction?

    Thanks

    h4x0rz3d

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    Please ask in the JavaSript frames works forum
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Nov 2006
    Posts
    9
    As advised I have created a thread in this section

    http://www.webdeveloper.com/forum/sh...l-Once-Success

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    Your code is a "framework" and that is what the JavaScript frameworks forum is for.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    i would try to execute or not the checkStatus() function from the

    Code:
    $.getJSON('progress.json', function(data) {
                    progress(data.progress, data.message);
                });
    response instead of setting an interval
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles