<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
相关推荐
憧憬成为web高手5 小时前
ACTF 12307复现wordbaby5 小时前
Axios 上传大文件崩溃:鸿蒙 RNOH 下 XHR 返回空响应头引发的"假失败"wordbaby6 小时前
React Native 列表分页实战:下拉刷新与上拉加载的工程化方案wordbaby6 小时前
脱离 Tab 栏的艺术:React Native 全屏子页面的导航架构实践陈随易6 小时前
Redis 8.8发布,一定要更新wordbaby7 小时前
React Native 新架构落地鸿蒙:跨三端政务级应用的工程实践与深度复盘晓说前端7 小时前
第一篇:为什么学TypeScript?—— 优势、场景与环境搭建excel8 小时前
为什么我推荐使用 Termius:现代 SSH 工具的完整体验ZC跨境爬虫8 小时前
模块化烹饪小程序开发日记 Day7:(菜谱详情接口开发与JSON数据读取全流程)এ慕ོ冬℘゜8 小时前
JS 前端基础面试题