<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
相关推荐
SLD_Allen16 分钟前
Go语言runtime全景计算机魔术师20 分钟前
NVIDIA 季度营收指引达 1080 亿美元,首次突破单季千亿大关数据狐(Datafox)35 分钟前
京东商品列表API技术解析与落地应用(含标准 JSON 示例)风萧何36 分钟前
架构艺术,是权衡的艺术。偷偷挖矿的牛马矿工37 分钟前
execa NodeJs新一代child_process的强力替代品YIAN37 分钟前
ESLint 新版 Flat Config 实战:团队代码风格统一,从这一份配置开始计算机魔术师38 分钟前
英伟达单季营收逼近千亿美元,黄仁勋放话:真实需求远超想象明月_清风40 分钟前
看完这段关于"全插件化架构"的技术分析后,我整理了一份笔记YIAN1 小时前
Next.js App Router 全栈实战:从 0 到 1 写一个 Todo 应用,前端后端一个项目搞定计算机魔术师1 小时前
英伟达预计 2028 财年营收同比增 70%,黄仁勋称实际需求远高于此