<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
相关推荐
Penge66616 分钟前
JavaScript 核心语法速通wangruofeng2 小时前
写给开发者的〈重构 UI〉实战笔记:从改好一个设置页开始,告别“程序员审美”程序员鱼皮2 小时前
格局!刚刚 DeepSeek 正式开放全家桶 API,咱都能复刻 DeepSeek App 了?ZhengEnCi2 小时前
登录页电脑端怎么设计才不空:5 种布局方案与可粘贴 AI 提示词Penge6662 小时前
JavaScript:DOM 树用户0044729538253 小时前
练歌镜:让眼镜听懂“循环这一句”Penge6663 小时前
CSS 核心布局指南:盒模型、Flex 布局与 Position 定位Penge6664 小时前
HTML基础:HTML 标签属性Penge6664 小时前
HTML 基础:块级元素、行内元素与行内块元素详解Penge6665 小时前
前端基础:html语义化