js定时器 数码时钟

Posted 跳刀玛利亚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js定时器 数码时钟相关的知识,希望对你有一定的参考价值。

 

开启定时器:  两种方式

setInterval(函数名,间隔时间)         间隔型

setTimeout(函数名,间隔时间)       延时型

 

示例1

<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>无标题</title>
<script>
function show(){
alert("a");
}
setInterval(show,1000);
</script>
<body>
</body>
</html>

``````````````````````````````````````````

示例2

<!DOCTYPE HTML>
<html>
<head>
<meta charset = "utf-8">
<title>无标题</title>
<script>
function show(){
alert("a");
}
setTimeout(show,1000);
</script>
<body>
</body>
</html>

·········································

关闭定时器:

clearInterval

clearTimeout

 

 

示例3

<!DOCTYPE HTML>
<html>
<head>
<meta charset = "utf-8">
<title>无标题</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="开启"/>
<input type="button" id="btn2" value="关闭"/>
</div>
</body>
</html>

``````````````````````````````````````````````````````````

示例:数码时钟

<!DOCTYPE HTML>
<html>
<head>
<meta charset = "utf-8">
<title>无标题</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]   也可以用   str.charAt(i)
}
}


setInterval(watch,1000);
watch();
}
</script>
<body style="background:black; color:white; font-size=50px;" >

<img src="images/0.png"/>
<img src="images/0.png"/>
:
<img src="images/0.png"/>
<img src="images/0.png"/>
:
<img src="images/0.png"/>
<img src="images/0.png"/>

</body>
</html>

//图片自己找个 从 1-9的图片吧  放在 image 文件夹下面

 



























































































以上是关于js定时器 数码时钟的主要内容,如果未能解决你的问题,请参考以下文章

基于51单片机定时器计数+74HC595移位寄存器+8位数码管时钟

基于51单片机定时器计数+2片74HC595联级+8位数码管时钟+按键修改时间

Proteus仿真基于STC15单片机简易数码管定时器时钟

2017.11.20 定时器与数码管

数码时钟

单片机STC15定时器(1)中断,数码管自动加1的电子钟,为啥不自动加1