0

Hey, I'm trying to grab a total amount of pages, and then grab the item_id's from each page. My code works but instead of each ajax request firing after the last one, they all run at once. How can I queue the calls so each call to get_page_items and then get_item is called 1 by 1.

function scraperFunction()
{
    $.get('./scraper/get_total_pages', { type: 'movie' }, function(data)
    {
        total_pages = data;
        total_items = total_pages * 6;

        for(page=1;page<=total_pages;page++)
        {
            $.get('./scraper/get_page_items', { type: 'movie', page: page }, function(json)
            {
                $.each(json, function(key, item_id)
                {
                    $.get('./scraper/get_item', { item_id: 'item_id' });
                });
            }, 'json');
        }
    });
}
Ryan
  • 1,356
  • 2
  • 10
  • 18

2 Answers2

2

You can use the Ajax Queue plugin, or use jQuery's built in queue support.

Community
  • 1
  • 1
Chetan
  • 46,743
  • 31
  • 106
  • 145
0

you can use the async: false option in $.ajax request

$.ajax({
    url: './scraper/get_page_items',
    data: { type: 'movie', page: page },
    async: false,
    dataType: 'json',
    success. function(json){
       ....
    }
})
bicccio
  • 384
  • 1
  • 19
  • yes, UI is blocked until the end of the grab function... sometimes this is not a real problem... – bicccio Apr 26 '11 at 21:49