vue手写计时器(正计时,倒计时)

<template>
    <div>
        <el-dialog
      append-to-body
      title="计时器"
      :visible.sync="showTimer"
      v-if="showTimer"
      :before-close="timerClose"
    >
      <div v-if="zx.show">
        <div  class="zx">
          <div class="number">{{formatTime(zx.num)}}</div>正计时
          <div class="icons">
            <span>
              <i @click="fn_zxplay" v-if="timerStatus == 'pause'" class="el-icon-video-play"></i>
              <i @click="fn_zxpause" v-if="timerStatus == 'play'" class="el-icon-video-pause"></i>
            </span>
            <span>
              <i @click="fn_zxreset" class="el-icon-refresh-right"></i>
            </span>
          </div>
        </div>
        <div style="margin: 20px auto;width: 200px;">
          <el-input-number type="number" v-model="zx.number" :min="0"></el-input-number>
        </div>
        <div class="ciic-form-btn">
          <el-button @click="fn_timesave(zx)" plain size="small" type="warning">确定</el-button>
        </div>
      </div>
      <div v-if="nx.show">
        <div class="nx">
          <div class="number">{{formatTime(nx.num)}}</div>倒计时
          <div class="icons">
            <span>
              <i @click="fn_nxplay" v-if="timerStatus == 'pause'" class="el-icon-video-play"></i>
              <i @click="fn_nxpause" v-if="timerStatus == 'play'" class="el-icon-video-pause"></i>
            </span>
            <span>
              <i @click="fn_nxreset" class="el-icon-refresh-right"></i>
            </span>
          </div>
        </div>
        <div style="margin: 20px auto;width: 200px;">
          <el-input-number type="number" v-model="nx.number" :min="0"></el-input-number>
        </div>
        <div class="ciic-form-btn">
          <el-button @click="fn_timesave(nx)" plain size="small" type="warning">确定</el-button>
        </div>
      </div>
    </el-dialog>
    </div>
</template>
<script>
    export default {
        data(){
              showTimer: false,
              timerStatus: 'pause',
            zx: {
                show: false,
                timer: null,
                num: 0,
               number: 0,
                index: 0,
            },
            nx: {
                show: false,
                timer: null,
                num: 0,
                number: 0,
                index: 0,
                bfNum: '',
              },
        }
        methods: {
            fn_zxplay(){
      this.timerStatus = 'play';
      this.zx.timer = setInterval(() => {
        this.zx.num++;
      }, 1000);
    },
    fn_zxpause(){
      this.timerStatus = 'pause';
      clearInterval(this.zx.timer);
    },
    fn_zxreset(){
      clearInterval(this.zx.timer);
      this.timerStatus = 'pause';
      this.zx.num = 0;
    },
    fn_nxplay(){
      this.timerStatus = 'play';
      this.nx.timer = setInterval(() => {
        this.nx.num--;
        if(this.nx.num == 0) {
          clearInterval(this.nx.timer);
          this.$message.warning('倒计时结束请输入结果')
        }
      }, 1000);
    },
    fn_nxpause(){
      this.timerStatus = 'pause';
      clearInterval(this.nx.timer);
    },
    fn_nxreset(){
      clearInterval(this.nx.timer);
      this.timerStatus = 'pause';
      let arr = this.nx.bfNum.split(':');
      this.nx.num = Number(arr[0] * 360) + Number(arr[1] * 60 + Number(arr[2]))
    },
        }
    }
<script>
<style scoped lang="scss">
    .zx, .nx {
  margin: 0 auto;
  width: 300px;
  height: 300px;
  border: 10px solid rgb(0,103,192);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  flex-direction: column;
  .number {
    font-size: 40px;
  }
  .icons {
    margin-top: 30px;
    width: 100%;
    text-align: center;
    span {
      margin: 0 5px;
      display: inline-block;
      width: 30px;
      height: 30px;
      text-align: center;
      line-height: 36px;
      border-radius: 50%;
      background-color: rgb(0,103,192);
      cursor: pointer;
      transition: all 0.2s;
      &:hover {
        transform: scale(1.1);
      }
      i {
        font-size: 26px;
        color: #fff
      }
    }
  }
}
</style>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值