-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path31 clearTimeout.html
76 lines (68 loc) · 2.57 KB
/
31 clearTimeout.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
<!DOCTYPE html>
<html lang="en">
<head><title>clearTimeout</title>
</head>
<body>
<!--
clearTimeout():
The clearTimeout() method clears a timer set with the setTimeout() method.
clearTimeout() does not return anything
Note
To clear a timeout, use the id returned from setTimeout():
var myTimeout = setTimeout(function, milliseconds);
Then you can to stop the execution by calling clearTimeout():
clearTimeout(myTimeout);
syntax of clearTimeout:
clearTimeout(id_of_setTimeout)
-->
<p id="displaytime"></p>
<button onclick="stoptime()">stop time</button>
<br>
<button id="startbtn" onclick="startCount()">Start</button>
<input type="text" id="displayCount">
<button onclick="stopCount()">Stop</button>
<button onclick="startfromZero()">Start From Zero</button>
<script>
var timeoutID=setTimeout(showTime,5000);
function showTime(){
let datetime=new Date(); //return new date and time
let time=datetime.toLocaleTimeString(); //return current local time
document.getElementById("displaytime").innerHTML=time;
console.log(time);
}
//How to showTime() to execute:
function stoptime(){
clearTimeout(timeoutID);
}
//This example has a "Start" button to start a timer, an input field for a counter,
// and a "Stop" button to stop the timer:
let counter=0;
let timeout;
let timer_on=0;
function timeCount(){
document.getElementById("displayCount").value=counter;
counter++;
timeout=setTimeout(timeCount,1000);
}
function startCount(){
if(timer_on==0){
timer_on=1;
timeCount();
}
}
function stopCount(){
clearTimeout(timeout);
timer_on=0;
document.getElementById("startbtn").innerHTML="Resume";
}
function startfromZero(){
clearTimeout(timeout);
document.getElementById("displayCount").value="0";
timer_on=0;
counter=0;
document.getElementById("startbtn").innerHTML="Start";
timeCount();
}
</script>
</body>
</html>