<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
相关推荐
莫西干式秃头12 分钟前
用 AI + threejs 写了个浏览器里的 3D 动作游戏《武来》:砍怪、放技能、开 SU7kyriewen26 分钟前
我测了 6 种防请求竞态的写法:只有 1 种真能打孟健29 分钟前
用 WorkBuddy 做个人名片网页,挂上开源设计技能后效果完全不同了IT_陈寒33 分钟前
SpringBoot自动配置的坑,这次终于爬出来了OpsEye1 小时前
企业大模型压测,需要重点关注哪些指标?LuHang2 小时前
从天地图切换到高德地图:一次真实的底图迁移实战用户360055579002 小时前
Kestrel_VLLM - 27 · 寄存器常驻与融合发射:两次「少发一次」前端小万2 小时前
用"道法术器"重构你和 AI 的协作方式鶴哥只手遮天2 小时前
Angular 22 源码级深度解析-第 06 章 模板指令全解:`ɵɵelementStart`、`ɵɵproperty`、`ɵɵadvance` 与绑定指令像我这样帅的人丶你还2 小时前
我html生成的doc文档为何在手机文档显示不了图片?