<div class="clockrun">
<span class="tuan" id="J_tuanTime"></span>
<span class="num" id="J_TimeHour">01</span>
<span class="dot">:</span>
<span class="num" id="J_TimeMin">38</span>
<span class="dot">:</span>
<span class="num" id="J_TimeSec">17</span>
<span class="dot">:</span>
<span class="num" id="J_TimeWSec">07</span></div>
<script>
var addTimer = function(){
var list = [],
interval;
return function(id,timeStamp){
if(!interval){
interval = setInterval(go,1);
}
list.push({ele:document.getElementById(id),time:timeStamp});
}
function go() {
for (var i = 0; i < list.length; i++) {
list[i].ele.innerHTML = changeTimeStamp(list[i].time);
if (!list[i].time)
list.splice(i--, 1);
}
}
//传入unix时间戳,得到倒计时
function changeTimeStamp(timeStamp){
var distancetime = new Date(timeStamp*1000).getTime() - new Date().getTime();
if(distancetime > 0){
//如果大于0.说明尚未到达截止时间
var ms = Math.floor(distancetime%1000);
var sec = Math.floor(distancetime/1000%60);
var min = Math.floor(distancetime/1000/60%60);
var hour =Math.floor(distancetime/1000/60/60%24);
if(ms<1){
ms = "0"+ ms;
}
if(sec<10){
sec = "0"+ sec;
}
if(min<10){
min = "0"+ min;
}
if(hour<10){
hour = "0"+ hour;
}
return hour;
return min;
return sec;
return ms;
}else{
//若否,就是已经到截止时间了
return "已截止!"
}
}
}();
addTimer("timer1",1483975403);//1月5日00点,unix时间戳自己去百度一下,就有的
</script>
<html> <body> <div class="clockrun"> <span class="tuan" id="J_tuanTime"></span> <span class="num" id="J_TimeHour">01</span> <span class="dot">:</span> <span class="num" id="J_TimeMin">38</span> <span class="dot">:</span> <span class="num" id="J_TimeSec">17</span> <span class="dot">:</span> <span class="num" id="J_TimeWSec">07</span> </div> <script> var timeStamp = 1583975403; //传入unix时间戳,得到倒计时 function changeTimeStamp(){ var distancetime = new Date(timeStamp*1000).getTime() - new Date().getTime(); if(distancetime > 0){ //如果大于0.说明尚未到达截止时间 var ms = Math.floor(distancetime%1000); var sec = Math.floor(distancetime/1000%60); var min = Math.floor(distancetime/1000/60%60); var hour =Math.floor(distancetime/1000/60/60%24); if(ms<1){ ms = "0"+ ms; } if(sec<10){ sec = "0"+ sec; } if(min<10){ min = "0"+ min; } if(hour<10){ hour = "0"+ hour; } var ss = [hour,min,sec,ms]; var dd = ["J_TimeHour","J_TimeMin","J_TimeSec","J_TimeWSec"]; for (var i = 0; i < dd.length; i++) { document.getElementById(dd[i]).innerHTML = ss[i]; } }else{ //若否,就是已经到截止时间了 document.getElementById("J_tuanTime").innerHTML = "已截止!"; } } setInterval(changeTimeStamp,1000); </script> </body> </html>
不知道你是到底想干啥,不过感觉你的方法里有错误...不知道这个是不是你想要的,把你的方法稍微改动了一下,觉得你没必要用闭包啊?
不是的我引用了一个jq
$.extend($.fn,{
fnTimeCountDown:function(d){
this.each(function(){
var $this = $(this);
var o = {
hm: $this.find(".hm"),
sec: $this.find(".sec"),
mini: $this.find(".mini"),
hour: $this.find(".hour"),
day: $this.find(".day"),
month:$this.find(".month"),
year: $this.find(".year")
};
var f = {
haomiao: function(n){
if(n < 10)return "00" + n.toString();
if(n < 100)return "0" + n.toString();
return n.toString();
},
zero: function(n){
var _n = parseInt(n, 10);//解析字符串,返回整数
if(_n > 0){
if(_n <= 9){
_n = "0" + _n
}
return String(_n);
}else{
return "00";
}
},
dv: function(){
//d = d || Date.UTC(2050, 0, 1); //如果未定义时间,则我们设定倒计时日期是2050年1月1日
var _d = $this.data("end") || d;
var now = new Date(),
endDate = new Date(_d);
//现在将来秒差值
//alert(future.getTimezoneOffset());
var dur = (endDate - now.getTime()) / 1000 , mss = endDate - now.getTime() ,pms = {
hm:"000",
sec: "00",
mini: "00",
hour: "00",
day: "00",
month: "00",
year: "0"
};
if(mss > 0){
pms.hm = f.haomiao(mss % 1000);
pms.sec = f.zero(dur % 60);
pms.mini = Math.floor((dur / 60)) > 0? f.zero(Math.floor((dur / 60)) % 60) : "00";
pms.hour = Math.floor((dur / 3600)) > 0? f.zero(Math.floor((dur / 3600)) % 24) : "00";
pms.day = Math.floor((dur / 86400)) > 0? f.zero(Math.floor((dur / 86400)) % 30) : "00";
//月份,以实际平均每月秒数计算
pms.month = Math.floor((dur / 2629744)) > 0? f.zero(Math.floor((dur / 2629744)) % 12) : "00";
//年份,按按回归年365天5时48分46秒算
pms.year = Math.floor((dur / 31556926)) > 0? Math.floor((dur / 31556926)) : "0";
}else{
pms.year=pms.month=pms.day=pms.hour=pms.mini=pms.sec="00";
pms.hm = "000";
//alert('结束了');
return;
}
return pms;
},
ui: function(){
if(o.hm){
o.hm.html(f.dv().hm);
}
if(o.sec){
o.sec.html(f.dv().sec);
}
if(o.mini){
o.mini.html(f.dv().mini);
}
if(o.hour){
o.hour.html(f.dv().hour);
}
if(o.day){
o.day.html(f.dv().day);
}
if(o.month){
o.month.html(f.dv().month);
}
if(o.year){
o.year.html(f.dv().year);
}
setTimeout(f.ui, 1);
}
};
f.ui();
});
}
});
</script>
<div id="fnTimeCountDown" data-end="2018/07/08 18:45:13">
<span class="hour">00</span>:
<span class="mini">00</span>:
<span class="sec">00</span>:
<span class="hm">00</span>
</div>
<script type="text/javascript">
$("#fnTimeCountDown").fnTimeCountDown("2018/07/08 18:45:13");
</script>