<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
相关推荐
爱勇宝8 小时前
大多数人不是在使用 AI 赚钱,而是在帮 AI 公司赚钱冬奇Lab8 小时前
每日一个开源项目(第143篇):page-agent - 纯 JS 的网页 GUI Agent,无需截图、无需插件、无需后端To_OC10 小时前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过IT_陈寒13 小时前
React的这个渲染问题连官方文档都没说清楚追逐时光者14 小时前
别再满网找零散工具了,腾讯 QQ 浏览器这个“帮小忙”工具箱真能省时间To_OC16 小时前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了Asmewill16 小时前
grep&curl命令学习笔记spmcor16 小时前
身份证读卡“无感登录”方案实践:从手动点击到自动检测stringwu16 小时前
Flutter 开发必备:MVI 架构的高效实现指南用户21366100357217 小时前
Vue2组件化开发与父子通信