-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
194 lines (163 loc) · 5.3 KB
/
index.html
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE html>
<html lang="en" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Game Timer</title>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="author" content="Gregor McNish">
<!--
Testing
clock keeps going when screen off (like smacktalk)
Timer can keep going even when exit app, cause using date, not timing
Game timer
Usage
click colour to stop/start timer for that person
if one persons is started, clicking another person's automatically stops it
New Game; resets timers
on launch
checks local storage for current game
if null, means is ready for a new game
if filled, continues the counts
any state change writes to local storage
implementation
clock ==minutes/seconds counter
times[n] accumulator for player
current: current player
Possibilities
total game time
individuals as percentage
if timer is going, could press at end to count it as your time, timer restarts. Next person just presses it. could touch the timer to pause it, touch again to reset or start it... Might be simpler; cause in other case if someone's forgotten to press...
cache works on wombat, not sure about ae.
working revision 1.2
-->
<script type="text/javascript" charset="utf-8">
var timer=function(){
var times=[0,0,0,0,0,0,0]; //index 0 holds clock start time
var start,finish; //turn start/finish -- because can't rely on counter
var current; //player
var interval=1000; //how often clock runs
var now,t,i;
var c; //holds timer
function init(){
//is a game running? if so load, if not set up a new one
if (localStorage.length){
for (i=1;i<times.length;i++){
times[i]=parseInt(localStorage.getItem('t'+i));
document.getElementById('t'+i).value=timecount(times[i]);
}
current=parseInt(localStorage.getItem('current'));
start=parseInt(localStorage.getItem('start'));
if (current){
document.getElementById('t'+current).style.borderColor="white";
clock();//keep clock running
c=setInterval(clock,interval); //because start not 0
}
}
else {
newgame();
}
}
function newgame(){
localStorage.clear();
times=[0,0,0,0,0,0,0];
if (current){
document.getElementById('t'+current).style.borderColor="black";
}
clockreset();
current=0;
start=0;
store();
init();
}
function store(){
for (i=0;i<times.length;i++){
localStorage.setItem('t'+i,times[i]);
}
localStorage.setItem('current',current);
localStorage.setItem('start',start);
}
function timecount(seconds){
var m=Math.floor(seconds/60);
var s=seconds%60;
return m+":"+((s<10)?"0":"")+s;
}
function clockreset(){
document.getElementById('clock').value=timecount(0);
clearInterval(c);
}
function clock(){
now=new Date();
if (start===0){ //start clock
start=now.getTime();
localStorage.setItem('start',start);
c=setInterval(clock,interval);
}
document.getElementById('clock').value=timecount(Math.round((now.getTime()-start)/1000));
}
function turn(p){
//player button has been pressed
clockreset();
now= new Date();
if (current===0){
current=p;
document.getElementById('t'+current).style.borderColor="white";
clock();
} else {
finish=now.getTime();
console.log(typeof(times[current]),times[current],finish,start);
times[current]=times[current]+Math.round((finish-start)/1000); //parseInt because sometimes? treated as string??
start=0;
localStorage.setItem('t'+current,times[current]); //save state
// console.log (current+" finished");
document.getElementById('t'+current).style.borderColor="black";
document.getElementById('t'+current).value=timecount(times[current]);
if (current==p){
current=0;
} else {
current=p;
document.getElementById('t'+current).style.borderColor="white";
clock();
}
}
localStorage.setItem('current',current); //save state
}
return {
turn:turn,
newgame:newgame,
init:init
};
}();
</script>
<style>
body {font-family:Helvetica, Arial, Verdana, sans-serif;
margin:0;
padding:5px;
background:rgba(0,0,0,1);
text-align:center;
-webkit-text-size-adjust:300%;
}
#clock {width:80%;text-align:center;margin:0;}
#timers {width:100%;
}
input[type="button"] {
width:140px;
border:5px solid black;
padding:5px;
-webkit-appearance:square-button;
background:white;
}
input#new {width:240px;}
#t1 {background:yellow;}
#t2 {background:green;}
#t3 {background:blue;}
#t4 {background:orange;}
#t5 {background:purple;}
#t6 {background:red;}
</style>
</head>
<body onload="timer.init();">
<input type="text" id="clock" readonly>
<div id="timers"><input type="button" id="t1" value="0:00" onclick="timer.turn(1);"><input type="button" id="t2" value="0:00" onclick="timer.turn(2);"><input type="button" id="t3" value="0:00" onclick="timer.turn(3);"><input type="button" id="t4" value="0:00" onclick="timer.turn(4);"><input type="button" id="t5" value="0:00" onclick="timer.turn(5);"><input type="button" id="t6" value="0:00" onclick="timer.turn(6);"></div><input type="button" id="new" value="New Game" onclick="timer.newgame();">
</body>
</html>