使用vue写一个计时器

**

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

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

<script>  
//定时器 异步运行  
function hello(){  
alert("hello");  
}  
//使用方法名字执行方法  
var t1 = window.setTimeout(hello,1000);  
var t2 = window.setTimeout("hello()",3000);//使用字符串执行方法  
window.clearTimeout(t1);//去掉定时器  
</script>

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

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

一般情况下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>

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>

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

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Vue.js的计算属性和生命周期函数来实现分秒计时器。 首先,在Vue组件中定义一个计算属性来计算当前时间和结束时间之间的时间差,并将其显示在页面上。 ```html <template> <div> <p>倒计时:{{countdown}}</p> </div> </template> <script> export default { data() { return { endTime: new Date('2021-12-31 23:59:59').getTime() } }, computed: { countdown() { let nowTime = new Date().getTime() let timeDiff = this.endTime - nowTime let seconds = Math.floor((timeDiff / 1000) % 60) let minutes = Math.floor((timeDiff / 1000 / 60) % 60) return `${minutes}分${seconds}秒` } } } </script> ``` 然后,在组件的生命周期函数中,使用`setInterval()`函数来每秒更新一次计算属性中的时间差,以达到倒计时的效果。 ```html <template> <div> <p>倒计时:{{countdown}}</p> </div> </template> <script> export default { data() { return { endTime: new Date('2021-12-31 23:59:59').getTime(), timer: null } }, computed: { countdown() { let nowTime = new Date().getTime() let timeDiff = this.endTime - nowTime let seconds = Math.floor((timeDiff / 1000) % 60) let minutes = Math.floor((timeDiff / 1000 / 60) % 60) return `${minutes}分${seconds}秒` } }, mounted() { this.timer = setInterval(() => { if (this.countdown === '0分0秒') { clearInterval(this.timer) alert('时间到了!') } }, 1000) }, beforeDestroy() { clearInterval(this.timer) } } </script> ``` 最后,在组件的`mounted()`生命周期函数中启动定时器,在组件的`beforeDestroy()`生命周期函数中清除定时器。 这样,你就可以在Vue.js中创建一个简单的分秒计时器了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值