Ryan
Ryan

Reputation: 1338

Queue jQuery AJAX Calls

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');
        }
    });
}

Upvotes: 0

Views: 1464

Answers (2)

bicccio
bicccio

Reputation: 394

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){
       ....
    }
})

Upvotes: 0

Chetan
Chetan

Reputation: 48061

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

Upvotes: 2

Related Questions