vue 服务器端倒计时

<template>

  <span class="setInterval-box">{{time}}</span>

</template>

<script>

export default {

  name: 'Timer',

  data() {

    return {

      hour: 0,

      minute: 0,

      second: 0,

      timer: null

    }

  },

  props: {

    remainingTime: {

      type: String,

      default: '00:00:00'

    }

  },

  computed: {

    time() {

      return `${this.hour < 10 ? `0${this.hour}` : this.hour}:${parseInt(this.minute / 60, 0) < 10 ? `0${parseInt(this.minute / 60, 0)}` : parseInt(this.minute / 60, 0)}:${this.second < 10 ? `0${this.second}` : this.second}`

    }

  },

  mounted() {

    if (this.remainingTime) {

      const Array = this.remainingTime.split(':')

      this.hour = parseInt(Array[0], 0)

      this.minute = parseInt(Array[1], 0)

      this.second = parseInt(Array[2], 0)

      this.timeDown()

    }

  },

  methods: {

    timeDown() {

      let totalSecond = this.hour * 3600 + this.minute * 60 + this.second;

      const second2Time = (seconds) => {

        this.hour = parseInt(seconds / 3600, 0);

        this.minute = parseInt(seconds % 3600, 0);

        this.second = parseInt(seconds % 3600 % 60, 0);

      };

      this.timer = setInterval(() => {

        if (totalSecond) {

          totalSecond -= 1;

          second2Time(totalSecond)

        } else {

          clearInterval(this.timer)

        }

      }, 1000)

    }

  },

  beforeDestroy() {

    clearInterval(this.timer)

  },

}

</script>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • // 15分钟计时var timers = null;var initMax = parseInt(900);//...
    令武阅读 428评论 0 0
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,499评论 0 10
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,068评论 0 2
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,550评论 0 13
  • <!DOCTYPE html> 定时器弹框 .pop{ width: 400px; height: 300px; ...
    啊烟雨阅读 1,774评论 0 0