Turn on the timer: two ways
SetInterval (function name, interval time) interval type
SetTimeout (function name, interval time) delay type
Example 1
<! DOCTYPE html>
<meta charset = "Utf-8" >
<title> Untitled </title>
<script>
Function Show () {
Alert ("a");
}
SetInterval (show,1000);
</script>
<body>
</body>
``````````````````````````````````````````
Example 2
<! DOCTYPE html>
<meta charset = "Utf-8" >
<title> Untitled </title>
<script>
Function Show () {
Alert ("a");
}
SetTimeout (show,1000);
</script>
<body>
</body>
·········································
To turn off the timer:
Clearinterval
Cleartimeout
Example 3
<! DOCTYPE html>
<meta charset = "Utf-8" >
<title> Untitled </title>
<script>
Window.onload=function () {
var obtn1 = document.getElementById (' btn1 ');
var obtn2 = document.getElementById (' btn2 ');
var schedule1 = null;
Obtn1.onclick=function () {
Alert ("AA");
Schedule1 = setinterval (function () {
Alert ("Schedule1 is running");
},1000);
};
Obtn2.onclick=function () {
Clearinterval (SCHEDULE1);
};
}
</script>
<body>
<div>
<input type= "button" id= "Btn1" value= "on"/>
<input type= "button" id= "btn2" value= "Off"/>
</div>
</body>
``````````````````````````````````````````````````````````
Example: Digital clock
<! DOCTYPE html>
<meta charset = "Utf-8" >
<title> Untitled </title>
<script>
Window.onload=function () {
var IMGs = document.getelementsbytagname (' img ');
function ToDouble (n) {
if (n<10) {
Return ' 0 ' +n;
}else{
Return ' +n;
}
}
function Watch () {
var odate = new Date ();
var str = todouble (odate.gethours ()) + ToDouble (odate.getminutes ()) + ToDouble (Odate.getseconds ());
for (var i = 0; i < imgs.length;i++) {
IMGS[I].SRC = "images/" +str[i]+ ". png"; Str[i] can also be used Str.charat (i)
}
}
SetInterval (watch,1000);
Watch ();
}
</script>
<body style= "Background:black; Color:white; font-size=50px; ">
:
:
</body>
Find a picture of yourself from 1-9 and put it under the image folder.
JS Timer Digital Clock