Articoli con Tag ‘countdown’

Una classe countDown in Javascript

Nel 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
 * 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

1
2
3
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 );

Continua...

3D CountDown con FIVe3D

Ho scritto una classe CountDown per Actionscript 3.0. Per provarla ho creato un esempio sfruttando le capacità di rendering testuale di FIVe3D.

Clicca per aprire il filmato Flash

Continua...

How I Did It: scrivere un countdown in Flash

Per la serie “How I Did It” (ovvero come lo feci) ecco come realizzare un countdown in Flash per visualizzare, partendo da un data, quanto giorni, ore, minuti e secondi mancano ad un evento! Nel filmato Flash qui sotto, ad esempio, vediamo quanti giorni mancano al primo dell’anno! Possono essere utilizzati i menu a tendina per selezionare un diversa data:

Loading Flash Player...

Creiamo tre combo day_cmb, month_cmb ed year_cmb e li inizializziamo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// init combo
import mx.controls.ComboBox;
//
for (var i = 1; i < 32; i++) {
  day_cmb.addItem(i, i);
}
var sm:Array = Array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
for (var i = 0; i < 12; i++) {
  month_cmb.addItem(sm[i], i);
}
// potrei partire dall'anno attuale...
for (var i = 2007; i < 2020; i++) {
  year_cmb.addItem(i, i);
}
// imposto la data al 1 gennaio 2008
day_cmb.selectedIndex = 0; // -1
month_cmb.selectedIndex = 0; // -1
year_cmb.selectedIndex = 1;

Il cuore del sistema lo posizioniamo nell’evento onEnterFrame:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
this.onEnterFrame = function() {
  //
  var today:Date = new Date();
  var currentYear = today.getFullYear();
  var currentTime = today.getTime();
  //
  var targetDate:Date = new Date(year_cmb.selectedItem.data, month_cmb.selectedItem.data, day_cmb.selectedItem.data);
  var targetTime = targetDate.getTime();
  //
  var timeLeft = targetTime - currentTime;
  //
  var sec = Math.floor(timeLeft / 1000);
  var min = Math.floor(sec / 60);
  var hrs = Math.floor(min / 60);
  var days = Math.floor(hrs / 24);
  //
  sec = String(sec % 60);
  sec = (sec.length < 2) ? "0" + sec : sec;
  //  
  min = String(min % 60);
  min = (min.length < 2) ? "0" + min : min;
  //  
  hrs = String(hrs % 24);
  hrs = (hrs.length < 2) ? "0" + hrs : hrs;
  //  
  days = String(days);
  days = (days.length < 2) ? "0" + days : days;
  //
  var counter:String = days + ":" + hrs + ":" + min + ":" + sec;
  //
  time_txt.text = counter;
};

Continua...


Stop SOPA