rewrote startTimer (-34 LOC)

This commit is contained in:
Neale Pickett 2011-11-22 22:45:09 -07:00
parent 30bc757216
commit 6b409fbed9
4 changed files with 81 additions and 116 deletions

View File

@ -37,7 +37,7 @@
#jam.lowtime, #period.lowtime { #jam.lowtime, #period.lowtime {
background: #f24; background: #f24;
} }
#jam.ascending { #jam.timeout {
background: #044; background: #044;
} }
#jam.rotate { #jam.rotate {

View File

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<!-- Presentation Timer 2011 Neale Pickett --> <!-- Presentation Timer 2011 Neale Pickett -->
<!-- Placed in the public domain. --> <!-- Placed in the public domain. -->
<!-- Time-stamp: "2011-11-17 14:26:50 neale" --> <!-- Time-stamp: "2011-11-22 19:26:31 neale" -->
<html> <html>
<head> <head>
<title>LADD Scoreboard</title> <title>LADD Scoreboard</title>
@ -12,7 +12,7 @@
src: url(Mostwasted.ttf); src: url(Mostwasted.ttf);
} }
body { body {
background: #222 url(stone-cmp.jpg); background: #222;
color: #eee; color: #eee;
font-family: Most Wazted, fantasy; font-family: Most Wazted, fantasy;
font-size: 5em; font-size: 5em;

View File

@ -1,7 +1,7 @@
/* /*
* LADD Roller Derby Scoreboard * LADD Roller Derby Scoreboard
* Copyright © 2011 Neale Pickett <neale@woozle.org> * Copyright © 2011 Neale Pickett <neale@woozle.org>
* Time-stamp: <2011-11-22 14:01:38 neale> * Time-stamp: <2011-11-22 22:35:40 neale>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -17,145 +17,107 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
/* State names */
var STARTUP = 0; var STARTUP = 0;
var JAM = 1; var JAM = 1;
var ROTATE = 2; var ROTATE = 2;
var TIMEOUT = 3; var TIMEOUT = 3;
var BREAK = 4;
var state = STARTUP; var state = STARTUP;
function pad(i) { // Create a timer on [element].
if (i < 10) { // If [tenths] is true, show tenths of a second.
return "0" + i; // If [callback] is defined, call it when time runs out.
} else { function startTimer(element, tenths, callback) {
return i;
}
}
// Start a timer on [element] for [duration] milliseconds.
function startTimer(element, precision, duration, callback) {
var beginning;
var itimer; var itimer;
var precmult = 1; var startTime;
var classname; var duration = 0;
var className;
var display = function () { // Re-calculate and update displayed time
function refresh() {
var remain = element.remaining(); var remain = element.remaining();
var min = Math.floor(Math.abs(remain / 60000)); var min = Math.floor(Math.abs(remain / 60000));
var sec = Math.abs(remain / 1000) % 60; var sec = (Math.floor(Math.abs(remain / 100)) / 10) % 60;
var t = "";
/* Scale sec to precision, since toFixed only rounds */ // Set classes
sec = Math.floor(sec * precmult) / precmult; element.className = className;
if (! itimer) {
if (itimer) { element.className += " paused";
element.className = classname; }
} else { if ((! className) && (remain <= 20000)) {
element.className = classname + " paused"; element.className += " lowtime";
}
if (! classname) {
if (! duration) {
element.className = "ascending";
} else if (remain <= 20000) {
element.className = "lowtime";
}
} }
// Has the timer run out?
if ((duration > 0) && (remain <= 0)) { if ((duration > 0) && (remain <= 0)) {
// Timer has run out
duration = 0; duration = 0;
element.stop(); sec = 0;
t = "0:0" + (0).toFixed(precision); clearInterval(itimer);
if (callback) callback(); itimer = undefined;
return; if (callback) {
callback();
}
} }
t += min + ":" + pad(sec.toFixed(precision));
// .toFixed() rounds, we want to truncate
if (! tenths) {
sec = Math.floor(sec);
} else {
sec = sec.toFixed(1);
}
// Zero-pad
if (sec < 10) {
sec = "0" + sec;
}
var t = min + ":" + sec;
if (t != element.innerHTML) { if (t != element.innerHTML) {
element.innerHTML = t; element.innerHTML = t;
} }
} }
// Is element timer running? // Return remaining time in milliseconds
element.running = function () {
return !!itimer;
}
// Is element a countdown timer?
element.descending = function () {
return !!duration;
}
element.d = function() {
return [beginning, duration];
}
// Return time on clock
element.remaining = function() { element.remaining = function() {
if (element.running()) { if (itimer) {
return beginning + duration - (new Date()).getTime(); var now = (new Date()).getTime();
return duration - (now - startTime);
} else { } else {
return duration; return duration;
} }
} }
// Stop (clear timer) // Set timer to [d] milliseconds.
// Put element into class [cn], if set.
element.set = function(t, cn) {
startTime = (new Date()).getTime();
duration = t;
className = cn;
refresh();
}
// Start timer
element.start = function() {
if (! itimer) {
startTime = (new Date()).getTime();
itimer = setInterval(refresh, 33);
}
refresh();
}
// Stop timer
element.stop = function() { element.stop = function() {
if (itimer) { if (itimer) {
duration = element.remaining();
clearInterval(itimer); clearInterval(itimer);
itimer = undefined; itimer = undefined;
} }
refresh();
} }
// Start
element.start = function () {
beginning = (new Date()).getTime();
if (itimer) {
return;
}
itimer = setInterval(display, 33);
}
// Unpause if paused
element.go = function () {
if (itimer) return;
element.start();
}
// Pause if unpaused
element.pause = function () {
if (! itimer) return;
element.stop();
duration -= (new Date()).getTime() - beginning;
display();
}
// Restart with a new time
element.reset = function (t, cn) {
classname = cn;
if (cn) {
element.className = cn;
}
self.stop();
duration = t;
beginning = (new Date()).getTime();
display(duration);
}
// Setup
for (var i = 0; i < precision; i += 1) {
precmult *= 10;
}
if (precision == undefined) {
precision = 1;
}
display();
} }
// Transition state machine based on state // Transition state machine based on state
@ -171,21 +133,21 @@ function transition(newstate) {
var jtext = document.getElementById("jamtext"); var jtext = document.getElementById("jamtext");
if (state == JAM) { if (state == JAM) {
pt.go(); pt.start();
jt.reset(120000); jt.set(120000);
jt.start(); jt.start();
jtext.innerHTML = "Jam"; jtext.innerHTML = "Jam";
} else if (state == ROTATE) { } else if (state == ROTATE) {
pt.go(); pt.start();
jt.reset(30000, "rotate"); jt.set(30000, "rotate");
jt.start(); jt.start();
jtext.innerHTML = "Rotation"; jtext.innerHTML = "Rotation";
} else if (state == TIMEOUT) { } else if (state == TIMEOUT) {
pt.pause(); pt.stop();
if (pt.remaining() <= 0) { if (pt.remaining() <= 0) {
pt.reset(1800000); pt.set(1800000);
} }
jt.reset(0); jt.set(0, "timeout");
jt.start(); jt.start();
jtext.innerHTML = "Timeout"; jtext.innerHTML = "Timeout";
} }
@ -269,7 +231,7 @@ function handle(event) {
sec = (sec * 60) + Number(v); sec = (sec * 60) + Number(v);
} }
e.reset(sec*1000); e.set(sec*1000);
} else { } else {
newstate = TIMEOUT; newstate = TIMEOUT;
} }
@ -365,8 +327,11 @@ function start() {
e("periodtext").innerHTML = "Period " + period; e("periodtext").innerHTML = "Period " + period;
c = Number(localStorage.rdsb_period_clock || 1800000); c = Number(localStorage.rdsb_period_clock || 1800000);
startTimer(p, 0, c); startTimer(p);
startTimer(j, 1, 120000); p.set(c);
startTimer(j, true);
j.set(120000);
save_itimer = setInterval(save, 1000); save_itimer = setInterval(save, 1000);
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 155 KiB