-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.js
More file actions
107 lines (101 loc) · 2.54 KB
/
Copy pathtimer.js
File metadata and controls
107 lines (101 loc) · 2.54 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
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
var endTime;
var bPaused;
function initForm()
{
bPaused = false;
var m = d3.select("#menu");
m.selectAll("*").remove();
m.append("input").attr("id", "timeinput").attr("value", "0:20:00");
m.append("button").attr("onclick", "startTimer()").html("Neustart");
m.append("button").attr("onclick", "pauseTimer()").html("Pause / Fortsetzen");
m.append("input").attr("id", "timesize").attr("value", "400").attr("onchange", "updateFontSizeAndCB()");
m.append("label").html("Textbox zeigen");
m.append("input").attr("type", "checkbox").attr("id", "sta").attr("onchange", "updateFontSizeAndCB()");
var t = d3.select("#timer");
t.selectAll("*").remove();
t.append("textarea").attr("rows", "1").attr("cols", 20).attr("id", "ta");
t.append("br");
t.append("label").attr("id", "timelabel")
updateFontSizeAndCB();
}
function startTimer()
{
bPaused = false;
updateFontSizeAndCB();
var t = d3.select("#timeinput")[0][0].value.split(":");
setTime(t);
animationTimer();
}
function setTime(t)
{
endTime = new Date();
endTime.setHours(endTime.getHours() + parseInt(t[0]));
endTime.setMinutes(endTime.getMinutes() + parseInt(t[1]));
endTime.setSeconds(endTime.getSeconds() + parseInt(t[2]));
}
function pauseTimer()
{
if(bPaused)
{
bPaused = false;
var t = d3.select("#timelabel")[0][0].innerHTML.split(":");
if(t.length == 2)
{
t[2] = t[1];
t[1] = t[0];
t[0] = 0;
}
setTime(t);
animationTimer();
}
else
bPaused = true;
}
function animationTimer()
{
var d = new Date();
if(d < endTime)
{
d.setHours(endTime.getHours() - d.getHours());
d.setMinutes(endTime.getMinutes() - d.getMinutes());
d.setSeconds(endTime.getSeconds() - d.getSeconds());
var s = getFormatedTimeString(d);
if (s == "02:00")
playSound();
d3.select("#timelabel")[0][0].innerHTML = getFormatedTimeString(d);
if(!bPaused)
requestAnimFrame(animationTimer);
}
else
{
d3.select("#timelabel")[0][0].innerHTML = "Ende";
playSound();
}
}
var audio = new Audio('Sounds/bell.mp3');
function playSound()
{
if(audio.paused)
audio.play();
}
function getFormatedTimeString(d)
{
var s = "";
if(d.getHours() != 0)
s = d.getHours() + ":";
if(d.getMinutes() < 10)
s += "0";
s += d.getMinutes() + ":";
if(d.getSeconds() < 10)
s += "0";
s += d.getSeconds();
return s;
}
function updateFontSizeAndCB()
{
var s = d3.select("#timesize")[0][0].value;
d3.select("#timelabel").attr("style", "font-size: " + s + "px");
d3.select("#ta").attr("style", "font-size: " + s / 10 + "px");
if(!d3.select("#sta")[0][0].checked)
d3.select("#ta")[0][0].style = "display: none";
}