<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
相关推荐
郑州光合科技余经理1 小时前
同城系统海外版:一站式多语种O2O系统源码博客zhu虎康2 小时前
uniApp 开发差点GDP2 小时前
模拟请求测试 Fake Rest API Test酒尘&3 小时前
Hook学习-上篇houyhea3 小时前
从香港竹脚手架到前端脚手架:那些"借来"的发展智慧与安全警示哈哈~haha3 小时前
Step 14: Custom CSS and Theme Colors 自定义CSS类Ndmzi3 小时前
Matlab编程技巧:自定义Simulink菜单(理解补充)勇气要爆发4 小时前
物种起源—JavaScript原型链详解我命由我123454 小时前
VSCode - VSCode 修改文件树缩进SoaringHeart4 小时前
Flutter组件封装:验证码倒计时按钮 TimerButton