<script type="text/javascript"> /* @用途:jQuery实现倒计时效果$(".time").countDown({time: "2015/12/1 10:00:00",type:0}) @参数:time: 结束时间,type:显示方式(0显示天数,1不显示天数) */ $.fn.countDown = function (opt) { var opt = $.extend( { time: null, type: 0, }, opt ); var edtime = new Date(opt.time).getTime(), //月份是实际月份-1 edsecond = (edtime - new Date().getTime()) / 1000; var eday = $(this).find(".day"), ehour = $(this).find(".hour"), eminute = $(this).find(".minute"), esecond = $(this).find(".second"); var timer = setInterval(function () { if (edsecond > 1) { let Html = ""; edsecond -= 1; var day = Math.floor(edsecond / 3600 / 24), hour = Math.floor((edsecond / 3600) % 24), minute = Math.floor((edsecond / 60) % 60), second = Math.floor(edsecond % 60); if (opt.type === 1) { hour = day * 24 + hour; } Html = `${opt.type === 0 ? day + "天" : ""}${ hour < 10 ? "0" + hour : hour }小时 ${minute < 10 ? "0" + minute : minute}分 ${ second < 10 ? "0" + second : second }秒`; $("#time").html(Html); } else { clearInterval(timer); } }, 1000); }; $(function () { $(".time").countDown({ time: "2023/3/26 18:00:00", type: 0, // 0显示天 1不显示天 }); }); </script>
复制
<span id="time"></span>
复制
效果