过几天要去面试:自己看到这个面试题,就写了一下:
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript"> window.onload = function () { //参数说明 elem:Dom元素ID字符串 a,b,c,d,e,f,g分别为年、月、日、时、分、秒、毫秒 var distDate = function _temp(elem,a,b,c,d,e,f,g){ a = a||0; b = b>=1?b||0; month = b-1; c = c||0; d = d||0; e = e||0; f = f||0; g = g||0; var nDistSecond = (new Date(a,month,c,d,e,f,g) - new Date())/1000, dates = Math.floor(nDistSecond/(60*60*24)), hours = Math.floor(nDistSecond%(60*60*24)/(60*60)), minutes = Math.floor(nDistSecond%(60*60*24)%(60*60)/60), seconds = Math.floor(nDistSecond%(60*60*24)%(60*60)%60); str = "距离" + a +"年"+ b + "月" + c + "日" + d + "时" + e + "分" + f + "秒" + "还有" + dates + "天" + hours + "时" + minutes +"分" + seconds + "秒"; var element = document.getElementById(elem); element.innerHTML = str; setTimeout(function (){ return _temp(elem,a,b,c,d,e,f,g); },1000); } //test distDate("time",2014,4,10);//显示现在距离2014年4月10日还有多长时间 } </script> </head> <body> <div id="time">这是倒计时的时间</div> </body> </html>