zoukankan      html  css  js  c++  java
  • javascript 计算倒计时

        function timeDown(second) {
            var month = '', day = '', hour = '', minute = '';
            if (second >= 86400 * 30) {
                month = Math.floor(second / (86400 * 30)) + '月';
                second = second % (86400 * 30);
            }
            if (second >= 86400) {
                day = Math.floor(second / 86400) + '天';
                second = second % (86400);
            }
            if (second >= 3600) {
                hour = Math.floor(second / 3600) + '小时';
                second = second % 3600;
            }
            if (second >= 60) {
                minute = Math.floor(second / 60) + '分';
                second = second % 60;
            }
            if (second > 0) {
                second = second ? second + '秒' : '';
            }
            return month + day + hour + minute + second;
        }

    如果想显示倒计时效果,可以使用如下代码调用:

    <!-- 引入jquery -->
    <script>
        $(function () {
            var second = 10000;
            $('.remain_time').html(timeDown(second));
            setInterval(function () {
                second--;
                $('.remain_time').html(timeDown(second));
            }, 1000);
        })
    </script>
    <span class="remain_time"></span>

     jquery插件形式:

                $.fn.timeDown = function (opt) {
                    var second = opt.second;
                    var tip = '已过期';
                    var $this = this;
                    self._timeDown = function (second) {
                        var month = '', day = '', hour = '', minute = '';
                        if (second >= 86400 * 30) {
                            month = Math.floor(second / (86400 * 30)) + '月';
                            second = second % (86400 * 30);
                        }
                        if (second >= 86400) {
                            day = Math.floor(second / 86400) + '天';
                            second = second % (86400);
                        }
                        if (second >= 3600) {
                            hour = Math.floor(second / 3600) + '小时';
                            second = second % 3600;
                        }
                        if (second >= 60) {
                            minute = Math.floor(second / 60) + '分';
                            second = second % 60;
                        }
                        if (second > 0) {
                            second = second ? second + '秒' : '';
                        } else {
                            return tip;
                        }
                        return month + day + hour + minute + second;
                    };
                    $this.html(self._timeDown(second));
                    setInterval(function () {
                        second--;
                        $this.html(self._timeDown(second));
                    }, 1000)
                };
    // 使用方式
    $('.remain_time').timeDown({second:1000,tip:'已过期'})
  • 相关阅读:
    04.安全-TLS1.2连接过程
    02.安全-证书和CA
    01.安全-加密
    00.https基本
    00.二叉树基本
    二分查找01.基本二本查找及其变种
    01.数据结构资料,时间复杂度空间复杂度(数据结构算法基本)
    从 CentOS 8 平滑迁移至 CentOS Stream
    identityserver使用记录
    vs2019 使用私钥进行连接时出错 ssh连接报错
  • 原文地址:https://www.cnblogs.com/eecjimmy/p/5029598.html
Copyright © 2011-2022 走看看