Posted By

Leech on 07/21/06


Tagged

class time timer


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

panatlantica
jkochis


CountDown v1.0


 / Published in: JavaScript
 

URL: http://jsfromhell.com/classes/countdown

Supplies countdown with pause and events. Created: 2006.06.23

  1. /*
  2. **************************************
  3. * CountDown Class v1.0 *
  4. * Autor: Carlos R. L. Rodrigues *
  5. **************************************
  6. */
  7. CountDown = function(){
  8. this.date = !(this.finished = this.paused = false);
  9. }
  10. CountDown.prototype.start = function(n, e, t, r){
  11. var o = this;
  12. r ? o.onResume && o.onResume(o.n) :
  13. (o.n = o.date ? new Date(n).getTime() : n, o.e = o.date ? new Date(e).getTime() : e,
  14. o.t = t, o.finished = false, o.onStart && o.onStart(o.n));
  15. o.d = e < n ? 1 : -1, o.paused = false, o.i = setInterval(function(){
  16. o.d * (o.n -= o.d * (o.date ? 1e3 : 1)) <= o.e * o.d &&
  17. (o.finished = !o.stop()) && !clearInterval(o.i) ||
  18. o.onUpdate && o.onUpdate(o.n);
  19. }, (o.t || 1) * 1e3);
  20. }
  21. CountDown.prototype.pause = function(t){
  22. var o = this;
  23. clearTimeout(o.x), o.paused ? o.start(o.n, o.e, o.t, 1) :
  24. (o.paused = !clearInterval(o.i), o.onPause && o.onPause(o.n),
  25. t && (o.x = setTimeout(function(){
  26. o.start(o.n, o.e, o.t, 1);
  27. }, t * 1e3)));
  28. }
  29. CountDown.prototype.stop = function(){
  30. var o = this;
  31. clearInterval(o.i), clearTimeout(o.x), o.onStop && o.onStop(o.n), o.n = 0;
  32. }

Report this snippet  

You need to login to post a comment.