<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
相关推荐
bai2734 小时前
path.join('C:\a', '\b') 给我 C:\a\b,path.resolve 给 C:\b:Windows 上 51 行路径实测kyriewen5 小时前
我写的 12 个 AI Skill,三个月后 8 个在吃空饷北城笑笑5 小时前
Vue 112 ,下篇:前端 dist 还原成 Vue3 + Vite 项目( krpano 全景接入与页面适配实战,Vue2 → Vue3 )我叫黑大帅6 小时前
搜“张三”把备注也带出来了?不用加高级筛选,如何让一个搜索框实现「选项直达」!KevinWang_6 小时前
iOS 开发计划(三) - 打印 JavaScript 调试日志如夏倾城8 小时前
WAF (Web Application Firewall,Web应用防火墙)图扑可视化9 小时前
基于图扑软件智慧制硅厂 Web SCADA 开发实践前端fighter9 小时前
2026年前端面试题总结(pdf版)谢小飞9 小时前
运维加了个参数,我们的网页挂掉了CoderYanger10 小时前
Java EE 进阶:12.Spring AOP(第二弹)