Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Class: Timer
- var Timer = function (callback) {
- // Property: Frequency of elapse event of the timer in milliseconds
- this.Interval = 1000;
- // Property: Whether the timer is enable or not
- this.Enable = new Boolean(false);
- // Event: Timer tick
- this.Tick = callback;
- // Member variable: Hold interval id of the timer
- var timerId = 0;
- // Member variable: Hold instance of this class
- var thisObject;
- // Function: Start the timer
- this.Start = function () {
- this.Enable = new Boolean(true);
- thisObject = this;
- if (thisObject.Enable) {
- thisObject.timerId = setInterval(
- function () {
- thisObject.Tick();
- }, thisObject.Interval);
- }
- };
- // Function: Stops the timer
- this.Stop = function () {
- thisObject.Enable = new Boolean(false);
- clearInterval(thisObject.timerId);
- };
- };
- // Namespace: Match rules and timings
- var Match = {
- Timers: {
- FirstHalf: new Timer(TimerTick),
- HalfTime: new Timer(TimerTick),
- SecondHalf: new Timer(TimerTick),
- TickCount: -1
- },
- Strings: {
- FirstHalf: 'First Half',
- HalfTime: 'Half Time',
- SecondHalf: 'Second Half',
- FullTime: 'Finished'
- },
- DisplayTime: function (t) {
- var m = parseInt(t / 60);
- var s = t % 60;
- return (m < 10 ? '0' + m : m) + ":" + (s < 10 ? '0' + s : s);
- }
- };
- // Function: Tick Event Handler (callback function)
- function TimerTick(timer) {
- // Document elements used.
- var TimerP = document.getElementById('time');
- var DisplayP = document.getElementById('display');
- // During First Half
- if (Match.Timers.FirstHalf.Enable == true) {
- if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 0 }
- if (Match.Timers.TickCount == 2700) {
- Match.Timers.FirstHalf.Stop();
- Match.Timers.TickCount = -1;
- Match.Timers.HalfTime.Start();
- } else {
- TimerP.innerHTML = Match.DisplayTime(Match.Timers.TickCount);
- DisplayP.innerHTML = Match.Strings.FirstHalf;
- Match.Timers.TickCount++;
- }
- }
- // During Half Time
- else if (Match.Timers.HalfTime.Enable == true) {
- if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 0 }
- if (Match.Timers.TickCount == 900) {
- Match.Timers.HalfTime.Stop();
- Match.Timers.TickCount = -1;
- Match.Timers.SecondHalf.Start();
- } else {
- TimerP.innerHTML = '45:00';
- DisplayP.innerHTML = Match.Strings.HalfTime + ' (' + Match.DisplayTime(900 - Match.Timers.TickCount) + ')';
- Match.Timers.TickCount++;
- }
- }
- // During Second Half
- else if (Match.Timers.SecondHalf.Enable == true) {
- if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 2700 }
- if (Match.Timers.TickCount == 5400) {
- TimerP.innerHTML = '90:00';
- DisplayP.innerHTML = Match.Strings.FullTime;
- Match.Timers.SecondHalf.Stop();
- Match.Timers.TickCount = -1;
- } else {
- TimerP.innerHTML = Match.DisplayTime(Match.Timers.TickCount);
- DisplayP.innerHTML = Match.Strings.SecondHalf;
- Match.Timers.TickCount++;
- }
- }
- }
- function KickOff() {
- var btn = document.getElementById('btnKickOff');
- btn.setAttribute('style','display: none;');
- Match.Timers.FirstHalf.Start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement