<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
相关推荐
不简说19 分钟前
JS 代码技巧 vol.5 — 10 个错误处理套路,从 try/catch 到 Error Boundarywordpress资料库22 分钟前
Next.js更适合移动开发 不适合web开发卷无止境32 分钟前
Quarkdown:赋予 Markdown 超能力的现代排版系统BerryS3N1 小时前
C++23新特性在CLion中的实战体验:从语法糖到生产力提升申君健24864182772022 小时前
# WebGPU 的渲染原理与 Shader随风123weber2 小时前
从前端响应式到后端背压:深度拆解生成式 UI(Generative UI)用户059540174462 小时前
用 Pytest 接管大模型记忆存储测试,状态一致性问题减少 90%IT_陈寒2 小时前
JavaScript的异步地狱里,我的Promise掉进了微任务陷阱嘟嘟07172 小时前
大前端工程师必学:用 BFF 架构搭建安全的 AI 流式应用meilindehuzi_a2 小时前
Vue 项目中的前端 BFF:跨域代理与流式请求实战