Posted in Javascript onMay 15, 2019
本文实例讲述了JavaScript页面倒计时功能。分享给大家供大家参考,具体如下:
效果图:
源码:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>3water.com JS倒计时</title> <style> h3{text-align:center;line-height:50px;height:50px;margin-top:100px;} #timer{text-align:center;} </style> </head> <body> <h3>倒计时</h3> <p id="timer">00:00:00</p> <script> var timeObj=document.getElementById("timer"); // var startTime=new Date(); // startTime=startTime.getTime(); // var endTime="2017-4-20 19:30:00"; // endTime=new Date(endTime.replace(/-/g,'/')).getTime(); // var diffTime = endTime-startTime; var diffTime=400000; //-----------------------------------倒计时start-------------------------------- function timeBack(){ var timer = setInterval(function(){ if(diffTime>=1000){ diffTime -= 1000; timeObj.innerHTML = formatDate(diffTime); } },1000) } timeBack(); function formatDate(maxtime) { var d = Math.floor(maxtime / (1000 * 60 * 60 * 24)); var h = Math.floor(maxtime / (1000*3600)) - (d * 24); var m = Math.floor(maxtime / (1000*60)) - (d * 24 *60) - (h * 60); var s = Math.floor(maxtime / (1000)) - (d * 24 *60*60) - (h * 60 * 60) - (m*60); var hour=(d*24)+h,minutes=m,seconds=s; if(hour>100){ hour=99 } if(hour < 10){ hour="0"+hour; } if (m < 10 ) { minutes = "0"+minutes }if(s <10){ seconds = "0"+seconds; } return hour+":"+minutes+":"+seconds; } </script> </body> </html>
感兴趣的朋友还可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.3water.com/code/HtmlJsRun测试上述代码运行效果。
JavaScript页面倒计时功能完整示例
- Author -
辣姐什么鬼声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@