Timer with resume


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package inca.utils {
  2.  
  3. import flash.utils.Timer;
  4.  
  5. public class TimerAdv extends Timer {
  6.  
  7. private var $__initDelay:Number = 0;
  8. private var $__pauseTime:Number = 0;
  9. private var $__acumulado:Number = 0;
  10. private var $__startingTime:Number = NaN;
  11. private var $__paused:Boolean = false;
  12.  
  13. public function TimerAdv(delay:Number, repeatCount:uint = 0){
  14. $__initDelay = delay;
  15. super(delay, repeatCount);
  16. }
  17.  
  18. public function get paused():Boolean{ return $__paused; }
  19.  
  20. override public function start():void{
  21. if($__paused){
  22. resume();
  23. }else{
  24. $__startingTime = new Date().getTime();
  25. super.start();
  26. }
  27. }
  28.  
  29. override public function reset():void{
  30. delay = $__initDelay;
  31. $__acumulado = 0;
  32. $__pauseTime = 0;
  33. super.reset();
  34. }
  35.  
  36. public function pause():void{
  37. if(isNaN($__startingTime)) throw new Error("You can't call TimerAdv.pause() without calling TimerAdv.start() first.")
  38. super.stop();
  39. $__paused = true;
  40. $__pauseTime = new Date().getTime();
  41. }
  42.  
  43. public function resume():void{
  44. $__paused = false;
  45. delay = Math.max(0, ($__initDelay - (($__pauseTime - $__startingTime) + $__acumulado)));
  46. $__acumulado += ($__pauseTime - $__startingTime);
  47. start();
  48. }
  49.  
  50. }
  51.  
  52. }

URL: http://blog.funciton.com/2009/09/timer-with-a-pause-and-resume.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.