• 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:'已过期'})
  • 相关阅读:
    10 shell test命令
    9 shell 退出状态
    8 shell if else
    7 shell 数学运算
    6-x3 declare和typeset命令:设置变量属性
    6-x1 read命令:从键盘读取数据
    Bootstrap 有一个 class 属性叫做 well,它的作用是为设定的列创造出一种视觉上的深度感
    form-control给input添加这个class类后就会使用bootstrap自带的input框
    bootstrap文字居中!
    img-responsive class图片响应式
  • 原文地址:https://www.cnblogs.com/eecjimmy/p/5029598.html
Copyright © 2020-2023  润新知