Created
April 17, 2013 14:32
-
-
Save letronje/5404785 to your computer and use it in GitHub Desktop.
ajax-seq.js
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
console.clear(); | |
var urls = ['/url1', '/url2', '/url3', '/url4', '/url5' , '/url6']; | |
function randint(a, b){ return Math.floor((Math.random()*b)+a); } | |
var Renderer = function(){ | |
var data = []; | |
var nextIndex = 0; | |
return { | |
notify: function(index, url, json){ | |
data[index] = { url: url, json: json }; | |
console.log('got data for ' + url); | |
var i; | |
for(i = nextIndex; i<= urls.length-1;i++){ | |
if(!data[i]){ nextIndex = i; break; } | |
console.log('rendering data from ' + data[i].url); | |
console.log(data[i]); | |
} | |
if(data.length == urls.length){ | |
console.log('all done'); | |
} | |
} | |
} | |
}(); | |
var ajaxer = function(index, url){ | |
setTimeout( function(){ | |
Renderer.notify(index, url, {msg: 'Done with ' + url}); | |
}, randint(1, 3000)); | |
} | |
for(u in urls){ | |
ajaxer(u, urls[u]); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment