<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
相关推荐
@atweiwei3 小时前
LangChainRust 0.33.0 发布:让 Agent 应用更容易运行、观察和评测yuki蜜语4 小时前
「阴升阳降」不符合物理常识?我用一台热虹吸做了个可拆解的 3D 模型ZJPRENO4 小时前
一个被身份证认证逼出来的开源项目:AI 辅助开发实战GoodStudyAndDayDayUp5 小时前
请求前端页面&traefik流程qq_274475159 小时前
第 28 篇:Promise 全解析:状态、then/catch/finally、静态方法 all/race/allSettled/anyGuWenyue11 小时前
昵称限 20 字,8 个 emoji 却超长:一个「字」在系统里有四种长度CV-deeplearning11 小时前
Chrome DevTools实战xurileidian11 小时前
第 30 篇 · 边玩边学Python · 猜数字小游戏 把判断循环异常串起来前端精髓12 小时前
三种方式实现 Agent 工具调用:OpenAI、LangChain 与 LangGraph国科安芯12 小时前
光链路捕获的那一刻:商业激光通信终端捕获跟踪伺服与探测前端的低噪声供电