<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
相关推荐
恋恋风尘hhh10 小时前
滑动验证码前端安全研究:以顶象(dingxiang-inc)为例萑澈16 小时前
Windows 7 运行 Electron 安装包报“不是有效的 Win32 应用程序”怎么办W.A委员会16 小时前
JS原型链详解懂懂tty17 小时前
React状态更新流程小码哥_常17 小时前
告别繁琐!手把手教你封装超实用Android原生Adapter基类她说彩礼65万17 小时前
C# 实现简单的日志打印skywalk816317 小时前
pytest测试的时候这是什么意思?Migrating <class ‘kotti.resources.File‘>一只蝉nahc17 小时前
vue使用iframe内嵌unity模型,并且向模型传递信息,接受信息状元岐18 小时前
C#反射从入门到精通子兮曰18 小时前
Bun v1.3.12 深度解析:新特性、性能优化与实战指南