<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js时分秒毫秒倒计时</title> </head> <body> <div></div> <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> <script> //js部分 function countTime(value) { //获取当前时间 var date = new Date(); var now = date.getTime(); //设置截止时间 //在pc端浏览器可以这样写 var endDate = new Date("2019-12-03 00:00:00"); //移动端必须这样写,因为ios不支持日期中间是-链接,会报错 //var endDate = new Date("2019/7/22 00:00:00"); var end = endDate.getTime(); //时间差 var differTime = end - now; //定义变量,h,m,s保存倒计时的时间 var h, m, s; if (differTime >= 0) { h = Math.floor(differTime / 1000 / 60 / 60); m = Math.floor(differTime / 1000 / 60 % 60); s = Math.floor(differTime / 1000 % 60); h = h < 10 ? ("0" + h) : h; m = m < 10 ? ("0" + m) : m; s = s < 10 ? ("0" + s) : s; var timeDom = "倒计时:" + h + "小时" + m + "分" + s + "秒"; $("#active_time").text(timeDom); //递归调用函数所以是延时器不是定时器 setTimeout(function () { countTime(value) }, 1000); } else { var timeDom ="00小时 00分 00秒"; $("#active_time").text(timeDom); }} countTime() </script> </body> </html>
js实现时分秒倒计时
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/2d931e9eae51816af3b31c554466db85.html