<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
相关推荐
Avan_菜菜3 小时前
AI 能写代码了,为什么我反而开始要求它先写文档?JieE2126 小时前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题JieE2127 小时前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解爱勇宝7 小时前
鸿蒙生态的下半场:开发者不只要能开发,还要能赚钱IT_陈寒10 小时前
SpringBoot这个自动配置坑我跳了三次kyriewen10 小时前
我用 AI 一周写完了整个项目,上线第一天就崩了——这是我踩过最贵的 5 个坑Larcher11 小时前
AI Loop:让AI像人一样自主完成任务的核心机制默_笙11 小时前
🃏 JS 只有 8 种数据类型,但我花了 2 天才搞懂 null 和 undefined 的区别牧艺11 小时前
从零到协同:构建类飞书在线文档系统的五个技术重难点jump_jump11 小时前
流式 HTML:从 htmx 片段装配到浏览器原生增量渲染