dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: Ajax / Stop setInterval Once Success

  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,589
    Please ask in the JavaSript frames works forum
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  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,589
    Your code is a "framework" and that is what the JavaScript frameworks forum is for.
    If your post falls off the page, bump it. ...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  5. #5
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,686
    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

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