• 计时器插件以及组件示例


    vue组件 

    <template>
      <div>
        <span>{{msg}}</span>
      </div>
    </template>
    <script>
    export default {
      data () {
        return {
          msg: ''
        }
      },
      props: {
        times: Number
      },
      created () {
        let _this = this
        let restTime = this.times
        /* eslint-disable */
        function time () {
          if (restTime === 0) {
            return
          }
          var startdays = Math.floor(restTime/3600/24)
          var starthours = Math.floor(restTime%86400/3600)
          var startMinutes = Math.floor(restTime%86400%3600/60)
          var startSec = Math.floor(restTime%86400%3600%60%60)
          if(startdays>0){
            _this.msg = startdays+''+starthours+''+startMinutes + '分钟'+startSec+''
          } else {
            _this.msg = starthours+''+startMinutes + '分钟'+startSec+''
          }
    
          restTime = restTime - 1
          var t=window.setTimeout(function () {
            time()
          }, 1000)
        }
        time()
        /* eslint-disable */
      }
    }
    </script>

    jq插件:

    ; (function ($, window, document, undefined) {
        'use strict';
        function timer(element, options) {
            this.element = element;
            this.options = {
                secondsNum: options.secondsNum || 1, //总秒数
                callback: options.callback // 回调函数
            };
            this.init();
        }
        timer.prototype = {
            constructor: timer,
            init: function () {
                this.createHtml();
            },
            createHtml: function () {
                var me = this;
                var secondsNum = me.options.secondsNum;
                function time() {
    
                    var starthours = Math.floor(secondsNum % 86400 / 3600)
                    var startMinutes = Math.floor(secondsNum % 86400 % 3600 / 60)
                    var startSec = Math.floor(secondsNum % 86400 % 3600 % 60 % 60)
                    startSec=startSec<10?'0'+startSec:startSec
                    startMinutes=startMinutes<10?'0'+startMinutes:startMinutes
                    var content = starthours + ':' + startMinutes + ':' + startSec
    
                    me.element.html(content);
                    if (secondsNum === 0) {
                        if (me.options.callback) {
                            me.options.callback(me.options.pageNum);
                        }
                        clearInterval(t)
                        return
                    }
                    secondsNum = secondsNum - 1
                    var t = window.setTimeout(function () {
                        time()
                    }, 1000)
                }
                time()
    
            }
        };
        $.fn.timer = function (options) {
            return new timer($(this), options);
        }
    })(jQuery, window, document);

    jq插件使用方式

    //使用格式
    $(".timer1").timer({
    secondsNum: $(".timer1").data("num")
    })
  • 相关阅读:
    机器学习-数据可视化神器matplotlib学习之路(四)
    [AspNetCore]CookieAuthentication禁用自动跳转到登录页
    [AspNetCore3.1] 使用Serilog记录日志
    [排序算法二]选择排序
    [排序算法一]冒泡排序
    Ocelot 网关 和 consul 服务发现
    AspNetCore3.0 和 JWT
    在AspNetCore3.0中使用Autofac
    【ElasticSearch+NetCore 第二篇】Nest封装
    【ElasticSearch+NetCore 第一篇】在Windows上安装部署ElasticSearch和ElasticSearch-head
  • 原文地址:https://www.cnblogs.com/nanacln/p/11009183.html
Copyright © 2020-2023  润新知