javascript实现倒计时程序
最近在网上看到一道这样的面试题:
题: 网页中实现一个计算当年还剩多少时间的倒数计时程序,要求网页上实时动态显示“××年还剩××天××时××分××秒”?
我实现了,发现挺有意思,下面把我的代码贴出来
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <script type="text/javascript"> function dayBetween(){ var today = new Date(); var enday = new Date(2016,11,31,0,0,0,0); //或 new Date("2016/12/31 0:0:0"); var between = enday-today; // console.log("between"+between+"\n"); var sec = Math.floor(between/1000); var day = Math.floor((Math.floor((Math.floor(sec/60))/60))/24); var hours = (Math.floor((Math.floor(sec/60))/60))%24; var minutes = (Math.floor(sec/60))%60 ; var seconds = sec%60; // console.log("result:"+day+"天"+hours+"时"+minutes+"分"+seconds+"秒"); var t = document.getElementById("time"); t.innerHTML = "离2016年12月31日0点还剩:"+"<b>"+day+"天"+hours+"时"+minutes+"分"+seconds+"秒"+"</b>"; } window.onload = function(){ setInterval(dayBetween,1000); }; </script> </head> <body> <p id="time"></p> </body> </html>