<template>
<div class="timer">
<p>{{ formatTime }}</p>
<button @click="startTimer" v-if="!isTiming">开始计时</button>
<button @click="stopTimer" v-else>停止计时</button>
</div>
</template>
<script>
export default {
data() {
return {
isTiming: false,
time: 0,
timer: null
}
},
computed: {
formatTime() {
const minutes = Math.floor(this.time / 60)
const seconds = this.time % 60
return `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`
}
},
methods: {
startTimer() {
this.isTiming = true
this.timer = setInterval(() => {
this.time++
}, 1000)
},
stopTimer() {
this.isTiming = false
clearInterval(this.timer)
}
}
}
</script>
<style>
.timer {
text-align: center;
font-size: 24px;
margin-top: 50px;
}
</style>
在Uni-app中实现计时器效果
蜕变菜鸟2024-08-27 17:46
相关推荐
折翼的恶魔18 分钟前
前端学习之样式设计IT_陈寒37 分钟前
JavaScript性能优化:3个被低估的V8引擎技巧让你的代码提速50%云飞云共享云桌面42 分钟前
SolidWorks服务器多人使用方案Red Car44 分钟前
javascript 性能优化实例一则艾小码1 小时前
从Hello World到变量数据类型:JavaScript新手避坑指南街尾杂货店&2 小时前
css word-spacing属性千叶寻-2 小时前
正则表达式2501_915918416 小时前
iOS 26 App 性能测试|性能评测|iOS 26 性能对比:实战策略光影少年7 小时前
angular生态及学习路线記億揺晃着的那天9 小时前
Vue + Element UI 表格自适应高度如何做?