Una classe countDown in Javascript
Lunedì 13 Ottobre, 2008Nel post 3D CountDown con FIVe3D (vedi anche How I Did It: scrivere un countdown in Flash), veniva proposta una classe per la creazione di un oggetto CountDown in Actionscript, eccone una versione simile in Javascript:
JavaScript:
-
/**
-
* CountDown Class
-
*
-
* @author Giovambattista Fazioli
-
* @email g.fazioli@undolog.com
-
* @web http://www.undolog.com
-
*
-
* @param dd (string) 'month day, year'
-
*
-
*/
-
function countDown( dd ) {
-
// init target time
-
var target = new Date( dd );
-
this.targetTime = target.getTime();
-
-
/**
-
* refresh countdown
-
*/
-
this.refresh = function() {
-
var today = new Date();
-
var currentTime = today.getTime();
-
// time left
-
this._leftMilliseconds = (this.targetTime - currentTime);
-
this._leftSeconds = Math.floor( this._leftMilliseconds / 1000 );
-
this._leftMinutes = Math.floor( this._leftSeconds / 60 );
-
this._leftHours = Math.floor( this._leftMinutes / 60 );
-
// no module
-
this.leftDays = Math.floor( this._leftHours / 24 );
-
// for print
-
this.leftMilliseconds = this._leftMilliseconds % 1000;
-
this.leftSeconds = this._leftSeconds % 60;
-
this.leftMinutes = this._leftMinutes % 60;
-
this.leftHours = this._leftHours % 24;
-
}
-
this.refresh();
-
}
Esempio
JavaScript:
-
var cd = new countDown( '1 1, 2009' );
-
// mostra quanti giorni, ore, minuti, secondi e millisecondi al primo gennaio 2009
-
document.write( cd.leftDays + "," + cd.leftHours + "," + cd.leftMinutes + "," + cd.leftSeconds + "," + cd.leftMilliseconds );




















