使用vue写一个计时器

2021-5-18    前端达人

**

首先我们要知道setTimeout和setInterval的区别

**
setTimeout只在指定时间后执行一次,代码如下:

<script>  
//定时器 异步运行  
function hello(){  
alert("hello");  
}  
//使用方法名字执行方法  
var t1 = window.setTimeout(hello,1000);  
var t2 = window.setTimeout("hello()",3000);//使用字符串执行方法  
window.clearTimeout(t1);//去掉定时器  
</script> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

setInterval以指定时间为周期循环执行,代码如下:

//实时刷新时间单位为毫秒  
setInterval('refreshQuery()',8000);   
/* 刷新查询 */  
function refreshQuery(){  
   $("#mainTable").datagrid('reload',null);  
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

一般情况下setTimeout用于延迟执行某方法或功能,
setInterval则一般用于刷新表单,对于一些表单的实时指定时间刷新同步
**

计时器

**
HTML代码

<div class="father">
        <ul>
            <li>{{one}}<span>:</span></li>
            <li>{{two}}<span>:</span></li>
            <li>{{three}}</li>
        </ul>
        <el-button type="primary" @click="startHandler">开始</el-button>
        <el-button type="primary" @click="endHandler">暂停</el-button>
    </div> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

JAVASCRIPT代码

<script>
export default {
  name: 'HelloWorld',
  data(){
      return {
        flag: null,
        one : '00', // 时
        two : '00', // 分
        three : '00', // 秒
        abc : 0, // 秒的计数
        cde : 0, // 分的计数
        efg : 0, // 时的计数
      }
  },
  props: {
    msg: String
  },
  mounted() {

  },
  methods:{
  // 开始计时
    startHandler(){
        this.flag = setInterval(()=>{
            if(this.three === 60 || this.three === '60'){
                this.three = '00';
                this.abc = 0;
                if(this.two === 60 || this.two === '60'){
                    this.two = '00';
                    this.cde = 0;
                    if(this.efg+1 <= 9){
                        this.efg++;
                        this.one = '0' + this.efg;
                    }else{
                        this.efg++;
                        this.one = this.efg;
                    }
                }else{
                    if(this.cde+1 <= 9){
                        this.cde++;
                        this.two = '0' + this.cde;
                    }else{
                        this.cde++;
                        this.two = this.cde;
                    }
                }
            }else{
                if(this.abc+1 <= 9){
                    this.abc++;
                    this.three = '0' + this.abc;
                }else{
                    this.abc++;
                    this.three=this.abc;
                }
            }

        },100)
    },
    // 暂停计时
    endHandler(){
        this.flag = clearInterval(this.flag)
    }
  }
}
</script> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

效果如下:
在这里插入图片描述




蓝蓝设计建立了UI设计分享群,每天会分享国内外的一些优秀设计,如果有兴趣的话,可以进入一起成长学习,请扫码蓝小助,报下信息,蓝小助会请您入群。欢迎您加入噢~~希望得到建议咨询、商务合作,也请与我们联系。

截屏2021-05-13 上午11.41.03.png


文章来源:csdn     作者:rock_23

分享此文一切功德,皆悉回向给文章原作者及众读者.
免责声明:蓝蓝设计尊重原作者,文章的版权归原作者。如涉及版权问题,请及时与我们取得联系,我们立即更正或删除。

蓝蓝设计www.lanlanwork.com )是一家专注而深入的界面设计公司,为期望卓越的国内外企业提供卓越的UI界面设计、BS界面设计 、 cs界面设计 、 ipad界面设计 、 包装设计 、 图标定制 、 用户体验 、交互设计、 网站建设 平面设计服务

分享本文至:

日历

链接

blogger

蓝蓝 http://www.lanlanwork.com

存档