<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
相关推荐
卷帘依旧7 分钟前
SSE(Server-Sent Events)完全指南码云之上8 分钟前
万星入坞:我们如何用三层插件体系干掉巨石应用kyriewen13 分钟前
一口气讲清楚 Monorepo、Turborepo、pnpm、Changesets 到底是什么?logo_2832 分钟前
Xpath语法规则的学习和使用IT_陈寒1 小时前
React性能优化踩的坑,这个错你可能也会犯zhangxingchao1 小时前
AI应用开发三:RAG技术与应用摘星小杨1 小时前
如何在前端循环调取接口,实时查询数据Hilaku2 小时前
从搜索排名到 AI 回答? 先聊一聊 AI 可见度工具 BuildSOM !zzmgc42 小时前
纯静态 + Web Worker + 虚拟滚动:我是怎么让浏览器吃下 10MB JSON 不卡的辰同学ovo2 小时前
用 Chrome DevTools MCP 给 AI 写的页面做“质检“