用vue怎样写倒计时按钮,代码是什么
Admin 2022-08-06 群英技术资讯 779 次浏览
这篇文章给大家介绍了“用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>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
JavaScript求两个数字中最大值的方法:1、利用Math对象的max()方法,语法“Math.max(数字1, 数字2);”;2、利用三元运算符,语法“数字1>数字2 ? 数字1 : 数字2;”。
方法与对象绑定的,不能单独引用,只能通过对象调用。功能独立,可单独引用。在方法中,this表示该方法所属的对象。在函数中,this代表整体对象。
这篇文章主要介绍了解决vue-cli输入命令vue ui没效果的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
这篇文章主要介绍了ElementUI下拉框选择后不显示值问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
这篇文章主要为大家介绍了node.js中Util模块的教程示例详解,带大家充分的了解node.js的util模块,有需要的朋友可以借鉴参考下,希望能够有所帮助
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008