<div>距离15点还有[<span id="spanTime">00:00:00</span>]</div>
function addZero(value) { return value < 10 ? "0" + value : value; } function timeDiff() { var nowTime = new Date(); var tarTime = new Date("2017/01/13 17:00:00"); var spanTime = tarTime - nowTime; if(spanTime<=0){ window.clearInterval(timer); return "活动结束了"; } var hour = Math.floor(spanTime / (1000 * 60 * 60)); //计算出时间 spanTime=spanTime-(hour*1000 * 60 * 60); //减去小时 var minute=Math.floor(spanTime/(1000*60)); //计算出分钟数 spanTime=spanTime-(minute*1000*60); //减去分钟 var second=Math.floor(spanTime/1000); //计算出秒数 return addZero(hour)+":"+addZero(minute)+":"+addZero(second); } var spanTime=document.getElementById("spanTime"); spanTime.innerHTML=timeDiff(); var timer=window.setInterval(function(){ spanTime.innerHTML=timeDiff(); },1000)