<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
相关推荐
KaMeidebaby3 小时前
卡梅德生物技术快报|PD1 单克隆抗体定制配套 N 糖全谱质控开发nuIl4 小时前
实现一个 Coding Agent(3):工具调用nuIl4 小时前
实现一个 Coding Agent(4):ReAct 循环nuIl4 小时前
实现一个 Coding Agent(1):一次 LLM 调用nuIl4 小时前
实现一个 Coding Agent(2):让 LLM 流式响应copyer_xyf4 小时前
Python 异常处理sugar__salt4 小时前
从栈队列数据结构到JS原型面向对象全解MageGojo4 小时前
随机文案模块怎么做?从接口封装到前端展示的完整实现思路独特的螺狮粉4 小时前
篮球集训班器具管理系统 - 鸿蒙PC Electron框架完整技术实现指南小妖6664 小时前
js 生成随机数技巧 Math.random().toString(36)