countDown Class in Javascript


/ Published in: JavaScript
Save to your folder(s)

A simple countDown class in Javascript:


Copy this code and paste it in your HTML
  1. /**
  2.  * CountDown Class
  3.  *
  4.  * @author Giovambattista Fazioli
  5.  * @web http://www.undolog.com
  6.  *
  7.  * @param dd (string) 'month day, year'
  8.  *
  9.  */
  10. function countDown( dd ) {
  11. // init target time
  12. var target = new Date( dd );
  13. this.targetTime = target.getTime();
  14. /**
  15. * refresh countdown
  16. */
  17. this.refresh = function() {
  18. var today = new Date();
  19. var currentTime = today.getTime();
  20. // time left
  21. this._leftMilliseconds = (this.targetTime - currentTime);
  22. this._leftSeconds = Math.floor( this._leftMilliseconds / 1000 );
  23. this._leftMinutes = Math.floor( this._leftSeconds / 60 );
  24. this._leftHours = Math.floor( this._leftMinutes / 60 );
  25. // no module
  26. this.leftDays = Math.floor( this._leftHours / 24 );
  27. // for print
  28. this.leftMilliseconds = this._leftMilliseconds % 1000;
  29. this.leftSeconds = this._leftSeconds % 60;
  30. this.leftMinutes = this._leftMinutes % 60;
  31. this.leftHours = this._leftHours % 24;
  32. }
  33. this.refresh();
  34. }

URL: http://www.undolog.com/2008/10/13/una-classe-countdown-in-javascript/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.