Category Archives: Javascript

Javascript Countdown

var countdown = {
    d : 0, h : 0, m : 0, s : 0,
    set : function(d, h, m ,s) {
        this.d = d;
        this.h = h;
        this.m = m;
        this.s = s;
        this.begin();
    },
 
    begin : function() {
 
    	if (this.d > 0 || this.h > 0 || this.m > 0 || this.s > 0) {
 
    		if (this.s == 0) {
 
    			if (this.m == 0) {
 
    				if (this.h == 0) {
 
    					if (this.d > 0) {
 
    						this.h = 23;
    						this.m = 59;
	    					this.s = 59;
    						this.d--;
 
    					} else {
 
    						this.h = 0;
	    					this.m = 0;
	    					this.s = 0;
    					}
 
    				} else {
 
    					this.h--;
    					this.m = 59;
    					this.s = 59;
    				}
 
    			} else {
 
    				this.m--;
    				this.s = 59;
    			}
 
    		} else {
 
    			this.s--;
    		}
 
	    	document.getElementById('day').innerHTML 	= (this.d <=9 ? '0' + this.d : this.d);
	    	document.getElementById('hour').innerHTML 	= (this.h <=9 ? '0' + this.h : this.h);
	    	document.getElementById('minute').innerHTML = (this.m <=9 ? '0' + this.m : this.m);
	    	document.getElementById('second').innerHTML = (this.s <=9 ? '0' + this.s : this.s);
 
    		setTimeout(function(){
	    		countdown.begin();
	    	}, 1000);
 
    	}
    }
}

usage:

Day:
<p id="day"></p>
<br />
Hour:
<p id="hour"></p>
<br />
Minute:
<p id="minute"></p>
<br />
Second:
<p id="second"></p>
<script type="text/javascript">
countdown.set(1, 0, 0, 3);
</script>
Page 1 of 11