js封装定时器 (同一页面多处引用)

demo1
 

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JS实现倒计时(天数,时,分,秒)</title>
 
</head>
<body>
 
<h2>剩余时间:</h2>
<div id="timer"></div>
<div id="timer1"></div>
<div id="timer2"></div>
<div id="timer3"></div>
 
 
<script language="javascript" type="text/javascript">
    function leftTimer(arg){
    var leftTime = (new Date(2019,01,11,11,11,11)) - (new Date()); //计算剩余的毫秒数
    var days = parseInt(leftTime / 1000 / 60 / 60 / 24  , 10); //计算剩余的天数
    var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时
    var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
    var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
    days = checkTime(days);
    hours = checkTime(hours);
    minutes = checkTime(minutes);
    seconds = checkTime(seconds);
	var els = arg.split(",")
	//console.log(arguments)
	for(var i in els){
		document.getElementById(els[i]).innerHTML = days+"天" + hours+"小时" + minutes+"分"+seconds+"秒";
	}
    setTimeout(function(){
    	leftTimer(arg)
    },1000);
}
/* setInterval(function(){
		leftTimer("timer","timer1","timer2","timer3")
	},1000); */
function checkTime(i){ //将0-9的数字前面加上0,例1变为01
    if(i<10)
    {
        i =i;
    }
    return i;
}
leftTimer("timer,timer1,timer2,timer3")
</script>
</body>
</html>

demo2

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JS实现倒计时(天数,时,分,秒)</title>
 
</head>
<body>
 
<h2>剩余时间:</h2>
<div id="timer"></div>
<div id="timer1"></div>
<div id="timer2"></div>
<div id="timer3"></div>
 
 
<script language="javascript" type="text/javascript">
    function leftTimer(arg){
    var leftTime = (new Date(2019,01,11,11,11,11)) - (new Date()); //计算剩余的毫秒数
    var days = parseInt(leftTime / 1000 / 60 / 60 / 24  , 10); //计算剩余的天数
    var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时
    var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
    var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
    days = checkTime(days);
    hours = checkTime(hours);
    minutes = checkTime(minutes);
    seconds = checkTime(seconds);
	// var els = arg.split(",")
	console.log(arguments)
	for(var i in arguments){
		document.getElementById(arguments[i]).innerHTML = days+"天" + hours+"小时" + minutes+"分"+seconds+"秒";
	}
    setTimeout(function(){
    	leftTimer(arg)
    },1000);
}
/* setInterval(function(){
		leftTimer("timer","timer1","timer2","timer3")
	},1000); */
function checkTime(i){ //将0-9的数字前面加上0,例1变为01
    if(i<10)
    {
        i =i;
    }
    return i;
}
leftTimer("timer",'timer1','timer2','timer3')
</script>
</body>
</html>

demo3

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JS实现倒计时(天数,时,分,秒)</title>
 
</head>
<body>
 
<h2>剩余时间:</h2>
<div id="timer"></div>
<div id="timer1"></div>
<div id="timer2"></div>
<div id="timer3"></div>
 
 
<script language="javascript" type="text/javascript">
    function leftTimer(arg){
    var leftTime = (new Date(2019,01,11,11,11,11)) - (new Date()); //计算剩余的毫秒数
    var days = parseInt(leftTime / 1000 / 60 / 60 / 24  , 10); //计算剩余的天数
    var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时
    var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
    var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
    days = checkTime(days);
    hours = checkTime(hours);
    minutes = checkTime(minutes);
    seconds = checkTime(seconds);
	// var els = arg.split(",")
	console.log(arguments)
	for(var i in arguments){
		document.getElementById(arguments[i]).innerHTML = days+"天" + hours+"小时" + minutes+"分"+seconds+"秒";
	}
    // setTimeout(function(){
    // 	leftTimer(arg)
    // },1000);
}
 setInterval(function(){
		leftTimer("timer","timer1","timer2","timer3")
	},1000); 
function checkTime(i){ //将0-9的数字前面加上0,例1变为01
    if(i<10)
    {
        i =i;
    }
    return i;
}
leftTimer("timer",'timer1','timer2','timer3')
</script>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值