Hi there,
I recently read this good example of using $.when and Deferred objects generated by $.get calls to Coordinate Coordinating multiple ajax requests (
http://goo.gl/H7g4g)
I tries this example and it works fine.
But I have the need to be be able to use $.when to coordinate 'n' number of ajax calls, where 'n' can be maybe 50 or 100 calls.
For e.g.:
I will look through and make 100 ajax calls using $.get, and get a collection of the deferred objects and then use $.when to handle all the callbacks data when all 100 calls are complete.
I tried doing this but it did not work (I also suspect this is wrong as when you .push into the deferredCollection array by calling getTweets the ajax call is already initiated and the response could have already come back before $.when is specified)
- var deferredCollection = [];
- deferredCollection.push(getTweets('austintexasgov'));
- deferredCollection.push(getTweets('greenling_com'));
- deferredCollection.push(getTweets('themomandpops'));
- deferredCollection.push(getTweets('anotherUserA'));
- deferredCollection.push(getTweets('anotherUserB'));
- // I just make 5 ajax calls here, but what if I make around 100??
- $.when( deferredCollection
- ).done(function(Args){
- // there is an error here trying to access Args
- });
- var getTweets = function(user){
- var url='http://twitter.com/status/user_timeline/' + user + '.json';
- return $.get(url, {count:5}, null, 'jsonp');
- }
Can someone please give me some ideas on how to do this?
Thanks in advance.