<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
相关推荐
byzh_rc8 分钟前
[微机原理与系统设计-从入门到入土] 微型计算机基础m0_471199638 分钟前
【小程序】订单数据缓存 以及针对海量库存数据的 懒加载+数据分片 的具体实现方式编程大师哥10 分钟前
Java webA小码哥11 分钟前
Vibe Coding 提示词优化的四个实战策略Murrays11 分钟前
【React】01 初识 React雪芽蓝域zzs14 分钟前
uniapp富文本rich-text大喜xi14 分钟前
ReactNative 使用百分比宽度时,aspectRatio 在某些情况下无法正确推断出高度,导致图片高度为 0,从而无法显示helloCat15 分钟前
你的前端代码应该怎么写电商API_1800790524715 分钟前
大麦网API实战指南:关键字搜索与详情数据获取全解析康一夏17 分钟前
CSS盒模型(Box Model) 原理