vue实现点击按钮倒计时
更新时间:2022年07月11日 09:03:35 作者:饥饿的帕尼尼
这篇文章主要为大家详细介绍了vue实现点击按钮倒计时,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了vue实现点击按钮倒计时的具体代码,供大家参考,具体内容如下
实现效果:
1.点击开始按钮启动计时

2.点击重置按钮计时恢复到00:00:00
3.点击暂停按钮暂停计时
Vue代码:
<template>
<div>
<div class="timeContainer">{{ time }}</div>
<a-button style="margin-right: 20px" type="primary" @click="start"
>开始</a-button
>
<a-button style="margin-right: 20px" type="primary" @click="reset"
>重置</a-button
>
<a-button type="primary" @click="end">暂停</a-button>
</div>
</template>
<script>
export default {
data() {
return {
flag: null,
hour: 0,
minute: 0,
second: 0,
time: "00:00:00",
};
},
methods: {
//开始计时
start() {
this.flag = setInterval(() => {
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.time =
this.complZero(this.hour) +
":" +
this.complZero(this.minute) +
":" +
this.complZero(this.second);
}, 1000);
},
//重新计时
reset() {
window.clearInterval(this.flag);
this.hour = 0;
this.minute = 0;
this.second = 0;
this.time = "00:00:00";
},
//暂停计时
end() {
this.flag = clearInterval(this.flag);
},
//补零
complZero(n) {
return n < 10 ? "0" + n : "" + n;
},
},
};
</script>
<style>
.timeContainer {
font-size: 40px;
margin-bottom: 10px;
}
</style>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
vue2 element 弹出框拖拽会出现一层阴影问题解决方法
这篇文章主要介绍了vue2 element 弹出框拖拽会出现一层阴影问题解决方法,因增加 draggable 属性导致我弹窗表单清空文本框时,从右向左选中字体会出现拖拽阴影效果,本文给大家介绍vue2 element 弹出框拖拽会出现一层阴影问题解决方法,感兴趣的朋友一起看看吧2024-01-01
前端vue框架select下拉数据量过大造成卡顿问题解决办法
这篇文章主要给大家介绍了关于前端vue框架select下拉数据量过大造成卡顿问题解决办法,文中通过示例代码介绍的非常详细,对大家学习或者使用select具有一定的参考借鉴价值,需要的朋友可以参考下2023-07-07


最新评论