For example, if you have a Search button that makes an AJAX request, and there's a loading spinner while waiting for that request, you could use this technique to make the spinner show for at least, say 500 milliseconds. I have noticed that a lot of AJAX search forms return so quickly that the loading spinner just barely flickers before the results display. It might be less jarring if you ensured that the spinner would show for at least 500 ms. Of course, you don't just want to add a 500 ms delay after the request returns, because then slow requests will get that much slower.
Multiple Simultaneous Ajax Requests (with one callback) in jQuery
21–30 of 31 posts
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#22This is the proper solution: http://css-tricks.com/multiple-simultaneous-ajax-requests-on...
$.when( // Get the css-tricks once $.get("http://css-tricks.com"), // get it twice $.get("http://css-tricks.com") ).then(function( csstricks1, csstricks2 ) { console.log( 'Fetched css tricks' ); console.log( csstricks1, csstricks2 ); }); csstricks1[0] is the content object and usually what you'll want - for example, if the response is json.
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#23Can't get enough of deferreds and promises? Want lots more examples of interesting, cunning and mind bending ways to use promises and deferreds? As luck would have it, this: http://shop.oreilly.com/product/0636920030508.do ...has just been published. (N.B. I'm one of the authors - all feedback would be most welcome and apologies for the shameless plug).
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#24Earlier quoted context omitted.
$.when( // Get the css-tricks once $.get("http://css-tricks.com"), // get it twice $.get("http://css-tricks.com") ).then(function( csstricks1, csstricks2 ) { console.log( 'Fetched css tricks' ); console.log( csstricks1, csstricks2 ); }); csstricks1[0] is the content object and usually what you'll want - for example, if the response is json.
Why would you fetch the same thing twice? Or is that just for you example and wouldn't be done in reality?
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#25Nowadays I use "async.js" (https://github.com/caolan/async): does the same, but with extra features.
It doesn't rely on jQuery unlike the snipplets of the article, so it's useful when you're aiming for vanilla or want to share code with Node.
It's far from the only library of this type, but I like it because it's reliable and has a good balance of simple yet tweakable for uncommon workflows.
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#26Like many, I had written myself a similar utility some years ago because "I need several stuffs that don't depend on each other, so they could be downloaded or calculated at the same time to get the end result faster" is a pattern that often comes up. Nowadays I use "async.js" ( https://github.com/caolan/async ): does the same, but with extra features. It doesn't rely on jQuery unlike the snipplets of the article, so…
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#27Can't get enough of deferreds and promises? Want lots more examples of interesting, cunning and mind bending ways to use promises and deferreds? As luck would have it, this: http://shop.oreilly.com/product/0636920030508.do ...has just been published. (N.B. I'm one of the authors - all feedback would be most welcome and apologies for the shameless plug).
Loving the book, seriously the examples are easy to follow and realy well paced.
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#28Can't get enough of deferreds and promises? Want lots more examples of interesting, cunning and mind bending ways to use promises and deferreds? As luck would have it, this: http://shop.oreilly.com/product/0636920030508.do ...has just been published. (N.B. I'm one of the authors - all feedback would be most welcome and apologies for the shameless plug).
Loving the book, seriously the examples are easy to follow and realy well paced.
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#29Can't get enough of deferreds and promises? Want lots more examples of interesting, cunning and mind bending ways to use promises and deferreds? As luck would have it, this: http://shop.oreilly.com/product/0636920030508.do ...has just been published. (N.B. I'm one of the authors - all feedback would be most welcome and apologies for the shameless plug).
I don't mean to shit on your book, but an entire book dedicated to jQuery deferreds? There is that much to say about them?
Re: Multiple Simultaneous Ajax Requests (with one callback) in jQuery
#30Or you can make a count-down latch of some kind: var num_results = 3; var results = [] var makeCallback = function(index) { return function(response) { num_results -= 1; results[index] = response; if (num_results > 0) { return; } // actually do stuff here, all results are loaded. }; }; $.get("/a", makeCallback(0)); $.get("/b", makeCallback(1)); $.get("/c", makeCallback(2)); // etc It's also not hard to make a generic…
Yes, you can reinvent wheels and avoid javascript best practices, and lose all benefits of async code control.
Promises are great, I totally agree (as long as they're A+ compliant). But they're not on every platform, and it's not (usually) worth building your own if it's not there.