计时器插件以及组件示例

时间:2019-06-12
本文章向大家介绍计时器插件以及组件示例,主要包括计时器插件以及组件示例使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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")
})

原文地址:https://www.cnblogs.com/nanacln/p/11009183.html