Click here to Skip to main content
15,886,774 members
Articles / Programming Languages / Javascript

Javascript Progress Bar

Rate me:
Please Sign up or sign in to vote.
3.97/5 (16 votes)
11 Oct 2008CPOL2 min read 112.8K   2.3K   45  
how to create a manual and automatic progress bar using JavaScript
//##############################################################  
//################ Start of progress Bar Class ####################
//##############################################################
//Global parameters
var numOfBullets = 19; // number of bullets in container
var timeInterval = 8;// time interval in seconds to use in auto mode
var progressBarClass; // global parameter to use in Auto mode with setTimeout (DONT DELETE)

//Classes
var ProgressBarClass = Class.create();
ProgressBarClass.prototype = {
    initialize: function(maxStep) 
    {  
    	progressBarClass = this; // set global parameter with the current class instance (dont delete this line. use in Auto method)
		this.timeInterval = timeInterval * 1000;
		this.isAuto = false;
		this.stop = false; // flag to indicate stop of auto increase if bullets
		this.currentStep = 0;		
		this.maxStep = maxStep;
		this.numOfBullets = numOfBullets; //total numer of bullets in progressBar
		this.leftBullets = numOfBullets; // internal count of left bullets to print
		this.OnStepChange = null; //callback to be activate when step is changed
		this.OnFinish = null; //callback to be activate when all steps is finish
    }, 
    //Set the container of the bullets
    SetProgressBarContainer : function(progressBarContainer)
    {
    	this.progressBarContainer = progressBarContainer;
    },
    //set the maxStep number. it also stop if active and reset parameters
    SetMaxStep : function(maxStep)
    {
    	this.Reset();
	   	this.maxStep = maxStep;
    },
    //Add new bullet to the current container
    AddBullet : function( bulletsToPrint, nextInterval )
    {         	
    	var newBullet = document.createElement('IMG');
		newBullet.setAttribute("id", "ImgRec");
		newBullet.src = "images/Progress_Bar_Fill.gif";		
		if(this.progressBarContainer != null)
			this.progressBarContainer.appendChild(newBullet);	
						
		if(bulletsToPrint > 0 )
		{
			setTimeout("progressBarClass.AddBullet(" + (bulletsToPrint - 1) + ", " + nextInterval + ")", nextInterval );
			return;
		}
			
		//increase the current step parameter
		this.currentStep++;
		
		//rize evevnt on step changed
		if(this.OnStepChange != null)
			this.OnStepChange(this.currentStep);	
		
		//Check if this was the last step			
		this.CheckIfEnd();
					
    }, 
    CheckIfEnd : function()
    {
    	if(this.currentStep >= this.maxStep)
		{			
			//rize evevnt on step changed
			if(this.OnFinish != null)
				this.OnFinish();
			return true;
		}
		return false
    },
    //increase the next step
	NextStep : function() 
	{ 		
		//Check if this was the last step
		if(this.CheckIfEnd())
		{
			return;
		}
		
		//calculate the amount of bullets to print and get the loest rount number
    	var bulletsToPrint = Math.floor(this.leftBullets / (this.maxStep - this.currentStep));
    	
    	//if we reached the last step then print the rest
    	if(bulletsToPrint > this.leftBullets)
    		bulletsToPrint = this.leftBullets;
    	
    	//set the left bullets to print
    	this.leftBullets = this.leftBullets - bulletsToPrint;
    	
		//Calculate the time intrval bettwen adding bullts in step	
    	var bulletsInterval = this.timeInterval / bulletsToPrint;
		
		//Start adding bullets for the current step in a loop of the current interval
		this.AddBullet((bulletsToPrint - 1) , bulletsInterval);		
    }, 
    //Start auto increment of bullets in a timer   
    Auto : function() 
	{ 			
		//if its stoped or reached the end then stop and return	
		if((this.stop == true && this.isAuto == true) || this.CheckIfEnd())
		{			
			this.isAuto = false;
			this.stop = false;
			return;
		}
						
		this.isAuto = true;	
		this.stop = false;
				
		this.NextStep();		
		//call this method again
		if(progressBarClass != null)
			setTimeout("progressBarClass.Auto()", this.timeInterval);		
    },
    //stop the auto mode
    Stop : function() 
	{ 	
		this.stop = true;
    },  
    //clear all progressBar parameters and clean bullets
    Reset : function()
    {
	    this.Stop();
    	this.isAuto = false;
		this.currentStep = 0;	
		this.leftBullets = this.numOfBullets;
		if(this.progressBarContainer != null)
			this.progressBarContainer.innerHTML = "";
    }
};

//##############################################################  
//################ End of progress Bar Class ######################
//##############################################################

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions