-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path59.timing.html
212 lines (159 loc) · 4.39 KB
/
59.timing.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html>
<head>
<title>Timing</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link type="text/css" href="styles.css" rel="stylesheet"/>
<link rel="stylesheet" href="https://www.w3schools.com/lib/w3.css">
<link href="prism.css" rel="stylesheet" />
<script src="scripts/prism.js"></script>
<style>
* {
box-sizing: border-box;
}
</style>
</head>
<body class="w3-margin">
<h1>Timing Events</h1>
<p>The window object allows execution of code at specified time intervals. These timing intervals are called timing events.</p>
<p>The two key methods to use with JavaScript are:</p>
<ul>
<li>setTimeout(function, milliseconds)</li>
<li>setInterval(function, milliseconds)</li>
</ul>
<div class="w3-panel w3-border-red w3-leftbar w3-pale-blue">
<p>The setTimeout() and setInterval() are both methods of the HTML DOM Window object.</p>
</div>
<h2>Timed Alert</h2>
<p>Set a timeout for an alert (3 seconds):</p>
<button onclick="setTimeout(myFunction1, 3000)">Set Alert</button>
<script>
function myFunction1() {
alert("Hello There!");
}
</script>
<pre><code class="language-javascript">
function myFunction1() {
alert("Hello There!");
}
</code></pre>
<hr />
<h2>Timed Text</h2>
<p>Display the number of seconds past at 2, 4, and 6 seconds:</p>
<p id="demo2"></p>
<button onclick="dispSeconds()">Display Seconds</button>
<script>
function dispSeconds() {
setTimeout(myTime1, 2000)
setTimeout(myTime2, 4000)
setTimeout(myTime3, 6000)
}
function myTime1() {
document.getElementById("demo2").innerHTML = "2 seconds";
}
function myTime2() {
document.getElementById("demo2").innerHTML = "4 seconds";
}
function myTime3() {
document.getElementById("demo2").innerHTML = "6 seconds";
}
</script>
<pre><code class="language-javascript">
function dispSeconds() {
setTimeout(myTime1, 2000)
setTimeout(myTime2, 4000)
setTimeout(myTime3, 6000)
}
function myTime1() {
document.getElementById("demo2").innerHTML = "2 seconds";
}
function myTime2() {
document.getElementById("demo2").innerHTML = "4 seconds";
}
function myTime3() {
document.getElementById("demo2").innerHTML = "6 seconds";
}
</code></pre>
<hr />
<h2>Continuous Timer</h2>
<p>Display a timer that runs "forever":</p>
<button onclick="setInterval(myCounter, 1000)">Start Time</button>
<p id="demo3"></p>
<script>
var s = 0;
function myCounter() {
document.getElementById("demo3").innerHTML = ++s;
}
</script>
<pre><code class="language-javascript">
var s = 0;
function myCounter() {
document.getElementById("demo3").innerHTML = ++s;
}
</code></pre>
<hr />
<h2>Continuous Timer with Stop Button</h2>
<p>Display a timer with a start and stop button:</p>
<button onclick="myTimer = setInterval(myCounter2, 1000)">Start</button>
<p id="demo4"></p>
<button onclick="clearInterval(myTimer)">Stop</button>
<script>
var c = 0;
function myCounter2() {
document.getElementById("demo4").innerHTML = ++c;
}
</script>
<pre><code class="language-javascript">
var c = 0;
function myCounter2() {
document.getElementById("demo4").innerHTML = ++c;
}
</code></pre>
<hr />
<h2>Clock</h2>
<p>Create a clock using a timing event:</p>
<button onclick="startTime()">Start Clock</button>
<p id="demo5"></p>
<script>
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById("demo5").innerHTML =
h + ":" + m + ":" + s;
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
//add zeros in front of numbers less than 10
if (i < 10) {
i = "0" + i
};
return i;
}
</script>
<pre><code class="language-javascript">
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById("demo5").innerHTML =
h + ":" + m + ":" + s;
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
//add zeros in front of numbers less than 10
if (i < 10) {
i = "0" + i
};
return i;
}
</code></pre>
</body>
</html>