To handle a list of resources (javascript files), you can utilize the following approach:
var loadedResources = 0;
var deferreds = [];
var resList = [ 'res1.js', 'res2.js' ];
$.each(resList, function(index, res) {
var load = $.ajax({
type: "GET",
url: res,
dataType: "script"
}).then(function() {
loadedResources += 1;
//Update progress bar logic here
//Utilize 'loadedResources' and 'resList.length'
//to determine progress bar width
});
deferreds.push(load);
});
$.when.apply(this, deferreds).then(function() {
//Hide or remove progress bar upon loading of all resources
});
The progress bar is updated each time a resource is successfully loaded. Once all resources are loaded, the progress bar can be hidden.
$.when.apply()
consolidates the deferred objects into one central deferred. Completion of this central deferred signifies completion of all contained deferreds as well.
Additionally, images or other content types can be included in the resource list, with necessary modifications to loading methods for specific resource types. Refer to this link for details.
For further insights on Deferred Objects, refer to this source.
Edit: Note that observing progress requires multiple resources in the list, not just one.