Unix时间戳 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jQuery倒计时效果,代码很简洁</title> </head> <body> <p class="endtime showtime" value="1951860800"></p> <p class="endtime showtime" value="1452860800"></p> <p class="endtime showtime" value="1453860800"></p> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> var time_current = (new Date()).valueOf(); //获取当前时间 $(function() { var dateTime = new Date(); var difference = dateTime.getTime() - time_current; setInterval(function() { $(".endtime").each(function() { var obj = $(this); var endTime = new Date(parseInt(obj.attr('value')) * 1000); var nowTime = new Date(); var nMS = endTime.getTime() - nowTime.getTime() + difference; var myD = Math.floor(nMS / (1000 * 60 * 60 * 24)); var myH = Math.floor(nMS / (1000 * 60 * 60)) % 24; var myM = Math.floor(nMS / (1000 * 60)) % 60; var myS = Math.floor(nMS / 1000) % 60; var myMS = Math.floor(nMS / 100) % 10; if (myD >= 0) { var str = myD + "天" + myH + "小时" + myM + "分" + myS + "." + myMS + "秒"; } else { var str = "已结束!"; } obj.html(str); }); }, 100); }); </script> </body> </html> 提示:你可以先修改部分代码再运行。 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jQuery倒计时效果,代码很简洁</title> </head> <body> <p class="endtime showtime" value="1951860800"></p> <p class="endtime showtime" value="1452860800"></p> <p class="endtime showtime" value="1453860800"></p> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> var time_current = (new Date()).valueOf(); //获取当前时间 $(function() { var dateTime = new Date(); var difference = dateTime.getTime() - time_current; setInterval(function() { $(".endtime").each(function() { var obj = $(this); var endTime = new Date(parseInt(obj.attr('value')) * 1000); var nowTime = new Date(); var nMS = endTime.getTime() - nowTime.getTime() + difference; var myD = Math.floor(nMS / (1000 * 60 * 60 * 24)); var myH = Math.floor(nMS / (1000 * 60 * 60)) % 24; var myM = Math.floor(nMS / (1000 * 60)) % 60; var myS = Math.floor(nMS / 1000) % 60; var myMS = Math.floor(nMS / 100) % 60; // if (myS<10){ // myS='0'+myS; // }; if (myMS<10){ myMS='0'+myMS; }; if (myD >= 0) { var str = myD + "天" + myH + "小时" + myM + "分" + myS + "秒" + myMS + "毫秒"; } else { var str = "已结束!"; } obj.html(str); }); }, 100); }); </script> </body> </html> 提示:你可以先修改部分代码再运行。 转载请注明:有爱前端 » jQuery倒计时效果,代码很简洁 喜欢 (0)or分享 (0)