C
C
ColdSpirit2015-07-25 16:54:46
JavaScript
ColdSpirit, 2015-07-25 16:54:46

How to cram an array of requests into $.when?

Good afternoon. I make requests to the page, add to the array. It is necessary that the when function work with an array, or something similar. I wrote a similar code in Greasemonkey (I gave the toaster as an example):

this.$ = this.jQuery = jQuery.noConflict(true);
$(function () {
$(document).ready(function()
{
        console.log('start');

        var link1 = "https://toster.ru/";
        var link2 = "https://toster.ru/questions";
        
        var queryArray = [];
        
        var query1 = $.get(link1).done(function($page){console.log("page1 loaded");});
        var query2 = $.get(link2).done(function($page){console.log("page2 loaded");});
        
        queryArray.push(query1);
        queryArray.push(query2);
                
        
        $.when(queryArray).done(function(){console.log("all pages loaded");});

        console.log('end');

});
});


Here is what he displays for me ("all pages loaded" should be at the very end):
start
 all pages loaded
end
 page1 loaded
 page2 loaded


If you set queries separately, then everything works fine, but this is not an option:
$.when(query1, query2).done(function(){console.log("all pages loaded");});


How to cram an array of requests into $.when, or are there any alternatives?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Zuev, 2015-07-25
@ColdSpirit

$.when.apply($, queryArray)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question