Created
November 6, 2013 18:25
-
-
Save tehsis/7341485 to your computer and use it in GitHub Desktop.
This file contains 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 foo = ["one", "two", "three", "four"]; | |
foo.forEach(function(value, index) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', '/ajax'); | |
// Save the element's index to which this request is | |
// related to. | |
xhr.index = index; | |
xhr.send(); | |
// Since the first element doesn't have a predecessor | |
// just process it withouth waiting any response. | |
processElement(foo[0]); | |
xhr.addEventListener('load', function() { | |
var next_element_index = this.index + 1; | |
if (foo[next_element_index] !== undefined) { | |
processElement(foo[next_element_index], this.response); | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment