As Hanlet mentioned, there are some commom scenarios that this can be used:
- You have a website and your page has a lot of images;
- You have a web app and your page has a lot of processes or ajax requests; or
- Both.
For number 1, I've seen people using image pre-loaders with two major benefits, first is that when page loads, all the images will just be there, and second is that images are usually the heavier part of a page load, so a basic percentage calculation on these would almost illustrate it. A simple javascript image pre-loader algorithm would loop through every img
tag that are on the document and create an Image
object, setting its src
atribute before inserting it on the page.
For number 2, I would make a observer-like Progress object like this:
var Progress = {
processes:{},
// Adds a process to keep track on
addProcess:function(name){
this.processes[name] = false; // false for non-completed
},
// Sets a process as completed, calls redrawProgress
setCompleted:function(name){
this.processes[name] = true;
this.redrawProgress();
},
redrawProgress:function(){ /* Updates the progress bar */ }
};
And then, inside each process, you should register it in the progress bar with the Progress.addProcess('myprocessname');
and call Progress.setCompleted('myprocessname');
when it finishes.
For number 3, I would try some way to know all the page's requests (including images, ajax data requests, external javascript calls, and the other processes), and mix it with the Progress
object solution in number 2, but I never tried this one.