Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (50 loc) · 1.85 KB
/
Copy pathscript.js
File metadata and controls
59 lines (50 loc) · 1.85 KB
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
if (window.localStorage) {
// Initialize timer or get previous value
localStorage.timer = (localStorage.timer || 0.0);
// Variable declaration
var chronoDiv = document.getElementById("chrono"),
playStopButton = document.getElementsByTagName("button")[1],
i = undefined;
// Show current chrono value
chronoDiv.innerHTML = (+localStorage.timer).toFixed(1);
// Start chrono and update each 0.1 seconds
function start() {
i = setInterval(function() {
localStorage.timer = +localStorage.timer + 0.1;
chronoDiv.innerHTML = (+localStorage.timer).toFixed(1);
}, 100);
}
function changeToStart() {
playStopButton.className = "start";
playStopButton.innerHTML = '<span id="button-icon" class="icon-play"></span>Start';
}
function changeToStop() {
playStopButton.className = "stop";
playStopButton.innerHTML = '<span id="button-icon" class="icon-stop"></span>Stop';
}
// Reset event
document.getElementsByName("button-reset")[0].addEventListener("click", function() {
window.clearInterval(i);
i = undefined;
localStorage.timer = 0.0;
chronoDiv.innerHTML = (+localStorage.timer).toFixed(1);
changeToStart();
});
// Start/stop event
document.getElementsByName("button-stop")[0].addEventListener("click", function() {
// Stop chrono
if (i) {
window.clearInterval(i);
changeToStart();
i = undefined;
}
// Start chrono
else {
changeToStop();
start();
}
});
} else {
// Error message
document.getElementsByTagName("header")[0].innerHTML += "<h3 style=\"color:red\">Sorry, your browser doesn't suppot LocalStorage</h3>";
}