2

I've been reading how the newer jQuery features Deferred / promise / when / then can be used to simplify acting on multiple asynchronous events such as AJAX queries.

But all of the examples I've been reading talk about a set number of queries that are known in advance.

$.when($.ajax("/page1.php"), $.ajax("/page2.php")).then(myFunc, myFailure);

(Borrowed from Coding Freak's answer here)

What about when you don't know them in advance, such as calling the same paged web API in a loop with different parameters, such as some features of the MediaWiki and StackExchange APIs have - is there any way to make use of these newer jQuery features in this case or do we have to go back to the old way?

Community
  • 1
  • 1
hippietrail
  • 15,848
  • 18
  • 99
  • 158
  • 2
    Have a look at my answer here: http://stackoverflow.com/questions/6647527/jquery-deferred-do-i-need-pipes-or-chains-to-achieve-this-pattern/6648219#6648219 – Felix Kling Dec 24 '11 at 09:14
  • 1
    Or this question / answer -> http://stackoverflow.com/questions/4785724/queue-ajax-requests-using-jquery-queue – Manse Dec 24 '11 at 09:19
  • 1
    possible duplicate of [jQuery $.when() with variable arguments](http://stackoverflow.com/questions/8011652/jquery-when-with-variable-arguments) – Felix Kling Dec 24 '11 at 11:24

1 Answers1

-3

You can call a function like ajaxloop('/page1.php')

function ajaxloop(url)
{
    $.ajax({
            url:url,
            success:function()
                       {
                     if(url=='/page1.php')
                      {
                       ajaxloop('/page2.php')
                      }
                     else
                     {
                      ajaxloop('/page1.php')
                       }
                 }
     });
}
DJ.
  • 394
  • 5
  • 5
  • give me specific question you are trying to looping your jquery or not. – DJ. Dec 28 '11 at 15:35
  • It looks like you need to know the exact number of iterations ahead of time and include inline code for those exact iterations. – hippietrail Dec 28 '11 at 15:50