<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
相关推荐
yuanyxh5 小时前
Mac 软件推荐万少5 小时前
AtomCode开发微信小程序《谁去呀》 全流程某人辛木6 小时前
Web自动化测试Kagol6 小时前
Superpowers GSD gstack AgentSkills深度测评excel7 小时前
JavaScript 字符串与模板字面量:从表象到本质理解京东云开发者7 小时前
当AI成为导演-如何用AI创作动漫短剧李白的天不白8 小时前
使用 SmartAdmin 进行前后端开发乘风gg8 小时前
🤡PUA AI Coding 工具 的 10 条终极语录学Linux的语莫8 小时前
Vue 3 入门教程怕浪猫9 小时前
第一章、Chrome DevTools Protocol (CDP) 详解