javascript - Graphs - Implementing ordered, asynchronous script loads? -
to increase performance there many ways load javascript asynchronously shown in so post.
however, in general, these methods not preserve ordering if need it, dependencies.
in ways can benefit of asynchronous loading preserving ordering when needed.
backbone dependencies example.
(require.js, jquery.js) -> backbone.js
is there implementation of promises or queues available libraries accomplish this?
it not head.js uses promises or queues yet.
easy solution: use promises. .ajax
in jquery provides promises of version 1.5.
if can't use third-party libraries, can this:
var resourcedata = {}; var resourcesloaded = 0; function loadresource(resource, callback) { var xhr = new xmlhttprequest(); xhr.onload = function() { var state = this.readystate; var responsecode = request.status; if(state == this.done && responsecode == 200) { callback(resource, this.responsetext); } }; xhr.open("get", resource, true); xhr.send(); } //assuming resources array of path names function loadresources(resources) { for(var = 0; < resources.length; i++) { loadresource(resources[i], function(resource, responsetext) { //store data of resource in resourcedata map, //using resource name key. increment //resource counter. resourcedata[resource] = responsetext; resourcesloaded++; //if number of resources have loaded equal //to total number of resources, means have //all our resources. if(resourcesloaded === resources.length) { //manipulate data in order desire. //everything need inside resourcedata, keyed //by resource url. ... ... } }); } }
Comments
Post a Comment