<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
相关推荐
程序员大阳21 分钟前
HTML+CSS+JS对话式实战(5)-活动管理页之查看报名名单的实现曦樂~37 分钟前
【Web Development】02 CSS风骏时光牛马1 小时前
后端服务接口开发与数据逻辑处理东鹏特饮1 小时前
经典打砖块(Breakout)游戏IT_陈寒1 小时前
Vite的HMR把我整不会了:一个生产环境的热更新坑Mh1 小时前
如何使用双缓冲优化视频切换risenav1 小时前
低头蓄力,抬头起跳:《萤虫跳跳》的 Rokid AIUI 开发实战ZhengEnCi2 小时前
如何看待 Fable 5.2 用纯代码生成 1.4MB 视觉作品:光追、水面、倒影、火光的程序化生成原理再吃一根胡萝卜2 小时前
DSH 插件的 Host 与 Client:为什么「只加载了 Host」,以及要不要自己写 Client 加载器parade岁月3 小时前
AI 写的前端代码为什么总像 5 年前的?Chrome 官方出手了