<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
相关推荐
今日无bug3 小时前
大模型的回答是怎么一个字一个字蹦出来的?前端流式输出解析计算机魔术师3 小时前
Anthropic 复盘 Claude 模型越权访问真实系统事件并改进对齐与安全措施掘金者阿豪3 小时前
Seedance 2.0/2.5 虚拟素材能跨 Key 共用吗?一次讲清 Asset ID、账号隔离与 SaaS 素材架构Jacky19993 小时前
Electron + Vue 3 桌面打字游戏实战:从 VSCode 扩展到独立应用的架构改造用户50093768390394 小时前
热敏小票 Web 打印:我在 58/80mm 上折腾的两周XGM4 小时前
一个前端新手的"顿悟"时刻:DOM树、渲染原理和JS动态渲染东方小月4 小时前
一篇文章带你深入拆解Skill的本质与工程实现,让你不再滥用Skillxy34534 小时前
Axure9.0 中继器遮罩的核心应用场景(精准适配列表交互)天道kabuto5 小时前
记一次 UnoCSS 样式离奇失效的踩坑复盘前端小张同学5 小时前
AI全栈开发最佳实践💐