Advertisement
Guest User

Timer.js

a guest
May 26th, 2012
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Class: Timer
  2. var Timer = function (callback) {
  3.     // Property: Frequency of elapse event of the timer in milliseconds
  4.     this.Interval = 1000;
  5.  
  6.     // Property: Whether the timer is enable or not
  7.     this.Enable = new Boolean(false);
  8.  
  9.     // Event: Timer tick
  10.     this.Tick = callback;
  11.  
  12.     // Member variable: Hold interval id of the timer
  13.     var timerId = 0;
  14.  
  15.     // Member variable: Hold instance of this class
  16.     var thisObject;
  17.  
  18.     // Function: Start the timer
  19.     this.Start = function () {
  20.         this.Enable = new Boolean(true);
  21.  
  22.         thisObject = this;
  23.         if (thisObject.Enable) {
  24.             thisObject.timerId = setInterval(
  25.             function () {
  26.                 thisObject.Tick();
  27.             }, thisObject.Interval);
  28.         }
  29.     };
  30.  
  31.     // Function: Stops the timer
  32.     this.Stop = function () {
  33.         thisObject.Enable = new Boolean(false);
  34.         clearInterval(thisObject.timerId);
  35.     };
  36.  
  37. };
  38.  
  39. // Namespace: Match rules and timings
  40. var Match = {
  41.  
  42.     Timers: {
  43.         FirstHalf: new Timer(TimerTick),
  44.         HalfTime: new Timer(TimerTick),
  45.         SecondHalf: new Timer(TimerTick),
  46.         TickCount: -1
  47.     },
  48.  
  49.     Strings: {
  50.         FirstHalf: 'First Half',
  51.         HalfTime: 'Half Time',
  52.         SecondHalf: 'Second Half',
  53.         FullTime: 'Finished'
  54.     },
  55.  
  56.     DisplayTime: function (t) {
  57.         var m = parseInt(t / 60);
  58.         var s = t % 60;
  59.         return (m < 10 ? '0' + m : m) + ":" + (s < 10 ? '0' + s : s);
  60.     }
  61. };
  62.  
  63. // Function: Tick Event Handler (callback function)
  64. function TimerTick(timer) {
  65.  
  66.     // Document elements used.
  67.     var TimerP = document.getElementById('time');
  68.     var DisplayP = document.getElementById('display');
  69.  
  70.     // During First Half
  71.     if (Match.Timers.FirstHalf.Enable == true) {
  72.         if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 0 }
  73.         if (Match.Timers.TickCount == 2700) {
  74.             Match.Timers.FirstHalf.Stop();
  75.             Match.Timers.TickCount = -1;
  76.             Match.Timers.HalfTime.Start();
  77.         } else {
  78.             TimerP.innerHTML = Match.DisplayTime(Match.Timers.TickCount);
  79.             DisplayP.innerHTML = Match.Strings.FirstHalf;
  80.             Match.Timers.TickCount++;
  81.         }
  82.     }
  83.  
  84.     // During Half Time
  85.     else if (Match.Timers.HalfTime.Enable == true) {
  86.         if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 0 }
  87.         if (Match.Timers.TickCount == 900) {
  88.             Match.Timers.HalfTime.Stop();
  89.             Match.Timers.TickCount = -1;
  90.             Match.Timers.SecondHalf.Start();
  91.         } else {
  92.             TimerP.innerHTML = '45:00';
  93.             DisplayP.innerHTML = Match.Strings.HalfTime + ' (' + Match.DisplayTime(900 - Match.Timers.TickCount) + ')';
  94.             Match.Timers.TickCount++;
  95.         }
  96.     }
  97.  
  98.     // During Second Half
  99.     else if (Match.Timers.SecondHalf.Enable == true) {
  100.         if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 2700 }
  101.         if (Match.Timers.TickCount == 5400) {
  102.             TimerP.innerHTML = '90:00';
  103.             DisplayP.innerHTML = Match.Strings.FullTime;
  104.             Match.Timers.SecondHalf.Stop();
  105.             Match.Timers.TickCount = -1;
  106.         } else {
  107.             TimerP.innerHTML = Match.DisplayTime(Match.Timers.TickCount);
  108.             DisplayP.innerHTML = Match.Strings.SecondHalf;
  109.             Match.Timers.TickCount++;
  110.         }
  111.     }
  112. }
  113.  
  114. function KickOff() {
  115.     var btn = document.getElementById('btnKickOff');
  116.     btn.setAttribute('style','display: none;');
  117.     Match.Timers.FirstHalf.Start();
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement