<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
相关推荐
Felven2 小时前
C. Make Them Equal智能收银研究所5 小时前
收银系统选型对比:美团、客如云、拉卡拉、收钱吧各适合什么门店滑动变滚动的蜗牛8 小时前
我发布了自己的个人小程序(诉述洞)ZYH_06019 小时前
Shell 第一阶段笔记:变量、引号与变量截取YIAN10 小时前
流式 TTS 和普通TTS到底有什么区别?一个文档教会你!时间的情敌10 小时前
Three.js GLTF 阀门 / 泵体模型加载、材质修改、状态联动(正常 / 故障 / 告警变色)掘金安东尼11 小时前
智能体包办工作时,批判性思维怎么办(Addy Osmani 全文转译)SameX13 小时前
HarmonyOS ArkTS Canvas 画环形进度条 animateTo + @Watch 驱动重绘 120 行实现bai27313 小时前
日志里的 [Object] 不是空对象:console.log 默认只展开两层,console.dir 也一样欧雷殿13 小时前
我的 AI 外包实战:用数字员工做项目的一人公司