<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
相关推荐
@大迁世界20 小时前
Firefox 本地模式:localhost 再见了 ?米米商聊1 天前
同名附件为什么被 Map 覆盖?重复键与一对多索引echome8881 天前
Python 异常处理:try、else、finally 的正确搭配时间的情敌1 天前
OpenLayers 实战|Vue3 + OpenLayers 完整工程案例(加载天地图、要素、弹窗、绘制、WFS)陈随易1 天前
下载神器!Motrix 停更三年后重写回归前端的日常1 天前
一个人+AI做情侣食谱小程序,0 费用的服务器计算机魔术师1 天前
谷歌搜索广告踩刹车,AI广告成为救命稻草总要学点什么1 天前
第81篇:数据可视化-PM2.5分布——让圆点的胖瘦替每座城市报出浓度IT_陈寒1 天前
Vue的v-for里用了个临时变量,页面直接挂了hunter_0761 天前
一次 iOS 真机签名 409 的完整排查:证书、设备、描述文件为什么总是"对不上"