利用js代码实现倒计时

在做购物网站时我们时常会用到倒计时来计算活动结束的时间,今天就和大家分享一下倒计时的实现

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div class="count-time">
        <span class="tis1"></span>
        <span class="tis2">天</span>
        <span class="tis3"></span>
        <span class="tis4">时</span>
        <span class="tis5"></span>
        <span class="tis6">分</span>
        <span class="tis7"></span>
        <span class="tis8">秒</span>
    </div>
    <script src="jquery-1.11.3.min.js"></script>
    <script>
        window.setInterval(function () {
            $(".count-time").countDown('2019/07/10 24:00:00');
        }, 1000);
        ; (function ($) {
            $.fn.extend({
                countDown: function (endTime) {
                    //取设定的活动结束时间距1970年1月1日之间的毫秒数
                    var end = new Date(endTime).getTime();
                    //取当前时间距1970年1月1日之间的毫秒数
                    var nowTime = new Date().getTime();
                    //结束时间与当前时间之间的毫秒差
                    var difference = end - nowTime;
                    if (difference > 0) {
                        //将毫秒差换算成天数
                        days = Math.floor(difference / 86400000);
                        difference = difference - days * 86400000;
                        //换算成小时
                        hours = Math.floor(difference / 3600000);
                        difference = difference - hours * 3600000;
                        //换算成分钟
                        minutes = Math.floor(difference / 60000);
                        difference = difference - minutes * 60000;
                        //换算成秒数
                        seconds = Math.floor(difference / 1000);
                        //不足10时,进行补零操作
                        if (hours < 10) {
                            hours = "0" + hours;
                        };
                        if (minutes < 10) {
                            minutes = "0" + minutes;
                        };
                        if (seconds < 10) {
                            seconds = "0" + seconds;
                        };
                        $(".tis1").html(days);
                        $(".tis3").html(hours);
                        $(".tis5").html(minutes);
                        $(".tis7").html(seconds);
                    } else {
                        //设定若是过了设置的活动结束时间,全部写成0天0时0分0秒
                        $(".tis1").html(0);
                        $(".tis3").html(0);
                        $(".tis5").html(0);
                        $(".tis7").html(0);
                    }
                }
            })
        })(jQuery);

    </script>
</body>

</html>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值