js 计时器

<template>
  <div class="wrapper">
    <h1 id=mytime>{{str}}</h1>
    <button id="stop" name="button" @click="stop">stop</button>
    <button id="start" name="button" @click="start">start</button>
    <button id="reset" name="button" @click="reset">reset</button>
  </div>
</template>
<script>
  export default {
    data(){
      return {
        h:0,//定义时,分,秒,毫秒并初始化为0;
        m:0,
        ms:0,
        s:0,
        time:0,
        str:'',
        mytime:''
 
      }
    },
    methods:{
       timer(){   //定义计时函数
    this.ms=this.ms+50;         //毫秒
    if(this.ms>=1000){
      this.ms=0;
      this.s=this.s+1;         //秒
    }
    if(this.s>=60){
      this.s=0;
      this.m=this.m+1;        //分钟
    }
    if(this.m>=60){
      this.m=0;
      this.h=this.h+1;        //小时
    }
    this.str =this.toDub(this.h)+":"+this.toDub(this.m)+":"+this.toDub(this.s)+""
   
  },
 
   reset(){  //重置
    clearInterval(this.time);
    this.h=0;
     this.m=0;
     this.ms=0;
     this.s=0;
    this.str="00:00:00";
  },
 
   start(){  //开始
     this.time=setInterval(this.timer,50);
  },
 
   stop(){  //暂停
    clearInterval(this.time);
  },
 
   toDub(n){  //补0操作
    if(n<10){
      return "0"+n;
    }
    else {
      return ""+n;
    }
  },
 
   toDubms(n){  //给毫秒补0操作
    if(n<10){
      return "00"+n;
    }
    else {
      return "0"+n;
    }
 }
  } 
  }
</script>
<style scoped>
  #mytime{
    background: #bbb;
    color: #fff;
    display: block;
  }
  .wrapper{
    text-align: center;
    width: 60%;
    margin: 250px auto;
  }
 
</style>

https://blog.csdn.net/qq_39313596/article/details/82745529

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值