Last active
December 26, 2015 08:39
-
-
Save luisgerhorst/7124295 to your computer and use it in GitHub Desktop.
Perform multiple async operations at once and handle the data when everything is finished.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var toLoad = 2, // number of chunks to receive | |
data = {}; // here we'll save the data, you could also use an array or multiple vars | |
$.ajax({ | |
url: 'a.txt', | |
error: function (jqXHR, textStatus, errorThrown) { | |
// handle error | |
}, | |
success: function (dataA, textStatus, jqXHR) { | |
data['a.txt'] = dataA; // so you can later access it in chunkReceived() | |
chunkReceived(); // that's important | |
} | |
}); | |
// same, you can do this as often as you want, just make sure you change toLoad | |
$.ajax({ | |
url: 'b.txt', | |
error: function (jqXHR, textStatus, errorThrown) { | |
// for real, alsways handle errors! | |
}, | |
success: function (dataB, textStatus, jqXHR) { | |
data['b.txt'] = dataB; | |
chunkReceived(); | |
} | |
}); | |
function chunkReceived() { | |
toLoad--; | |
if (!toLoad) { // execute when all data loaded | |
console.log(data['a.txt'], data['b.txt']); // example: that's how you access the data | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment