0

I'm trying to write a recursive jQuery statement that grabs some data and populates fields. However I don't want the next command to be executed until the current one is finished. I'm trying to do the below, however it doesn't seem to make it to the second run. Is there something I am doing wrong?

I kick it off by doing:

var arg = new Array();
arg[0]= 'id0';
arg[1]= 'id1';
arg[2]= 'id2';
arg[3]= 'id3';
arg[4]= 'id4';
arg[5]= 'id5';

grabMetrics('toggle=0', arg, 0);


function grabMetrics(url, locationAry, num){
alert(num);
$.ajax({
    type: "POST",
    url: "foo.php",
    data: url,
    success: function($this){
        $("#"+locationAry[num]).text($this);
        if(num<5){
            num++;
            grabMetrics('toggle='+num, locationAry, num);
        }
    }
});
}

Thank you in advance!

2 Answers 2

2

Change this:

if(num>5){

to this:

if(num<5){
Sign up to request clarification or add additional context in comments.

1 Comment

arg. sorry for wasting your time.
2

Shouldn't that be if(num < 5)?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.