AI智能
改变未来

JavaScript 实现倒计时

<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>倒计时</title></head><body><h1 id=\"timer\"></h1><script>function leftTimer(year, month, day, hour, minute, second) {var leftTime = (new Date(year, month - 1, day, hour, minute, second)) - (new Date()); //计算剩余的毫秒数var days = parseInt(leftTime / 1000 / 60 / 60 / 24); //计算剩余的天数var hours = parseInt(leftTime / 1000 / 60 / 60 % 24); //计算剩余的小时var minutes = parseInt(leftTime / 1000 / 60 % 60); //计算剩余的分钟var seconds = parseInt(leftTime / 1000 % 60); //计算剩余的秒数days = checkTime(days);hours = checkTime(hours);minutes = checkTime(minutes);seconds = checkTime(seconds);setInterval(\"leftTimer(2020,11,11,23,59,59)\", 1000); //传入目标时间document.getElementById(\"timer\").innerHTML = \"距离双十一狂欢购物节还有\" + days + \"天\" + hours + \"小时\" + minutes + \"分\" + seconds +\"秒\";}function checkTime(i) { //将0-9的数字前面加上0,例1变为01if (i < 10) {i = \"0\" + i;}return i;}leftTimer();</script></body></html>
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » JavaScript 实现倒计时