vue 计时器开始暂停重置继续

<template>
    <view>
      <view>
        <button @click="timeStart()">开始</button>
        <button @click="stop">暂停</button>
        <button @click="reset">重置</button>
        <button @click="timeStart">继续</button>
    </view>
    <view style="display:flex;align-items: center;justify-content: center;">
        {{str}}
    </view>
    </view>
 
</template>
 
<script>
    export default {
        data(){
            return {
                hour:0,//定义时,分,秒,毫秒并初始化为0;
                minute:0,
                ms:0,
                second:0,//秒
                time:'',
                str:'00:00:00',
            };
        },
    
        methods:{
            //开始计时
            timeStart(){
            this.time =setInterval(this.timer,50)
            },
            //
            timer () {//定义计时函数
            this.ms =this.ms +50        //毫秒
            if (this.ms >=1000) {
            this.ms =0
            this.second =this.second +1        //秒
                    }
            if (this.second >=60) {
            this.second =0
                      this.minute =this.minute +1        //分钟
                    }
            if (this.minute >=60) {
            this.minute =0
                      this.hour =this.hour +1        //小时
                    }
        //
        this.str =this.toDub(this.hour) +':' +this.toDub(this.minute) +':' +this.toDub(this.second)/*+""+this.toDubms(this.ms)+"毫秒"*/
                // document.getElementById('mytime').innerHTML=h+"时"+m+"分"+s+"秒"+ms+"毫秒";
         },
         //补零操作
         toDub (n) {//补0操作
                 if (n <10) {
         return '0' + n
         }else {
         return '' + n
         }
         },
         //暂停
         stop () {
         console.log('暂停')
         clearInterval(this.time)
         },
         //重置
         reset () {//重置
         clearInterval(this.time)
         // this.hour =0
         this.minute =0
         this.ms =0
         this.second =0
         this.str ='00:00:00'
         },
         }
         
              
    }
</script>
 
<style lang="scss">
    
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值