-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoreboard.js
65 lines (51 loc) · 1.62 KB
/
scoreboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Scoreboard Applet
// by cosmosd '15
var interval;
var t = 180;
function updateScore(add, target) {
var div = $(target);
var score = parseInt(div.html());
score += add;
if (score < 0) { score = 0; } // clip negative scores
$(target).html(score);
}
function resetScore(target) {
$(target).html(0);
}
// Helper, turns 175 into "2:55"
function timeToString(t) {
return Math.floor(t/60) + ":" + ("0" + t % 60).slice(-2);
}
// Called every second from startClock()
function decreaseTime() {
t -= 1;
if (t < 0) { t = 0; } // clip negative time
$(".clock").html(timeToString(t));
}
function startClock() {
clearInterval(interval);
interval = setInterval(function () { decreaseTime() }, 1000);
}
function pauseClock() {
clearInterval(interval);
}
function resetClock() {
clearInterval(interval);
input = $("#t");
t = parseInt(input.val());
$(".clock").html(timeToString(t));
}
$(document).ready(function() {
// Initialize
$(".clock").html(timeToString(t));
// Handlers
$(".increment-away").click(function() { updateScore(+1, ".away") });
$(".decrement-away").click(function() { updateScore(-1, ".away") });
$(".increment-home").click(function() { updateScore(+1, ".home") });
$(".decrement-home").click(function() { updateScore(-1, ".home") });
$(".reset-away").click(function() { resetScore(".away") });
$(".reset-home").click(function() { resetScore(".home") });
$(".start-clock").click(function() { startClock() });
$(".pause-clock").click(function() { pauseClock() });
$(".reset-clock").click(function() { resetClock() });
});