<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
相关推荐
幼儿园蛋小黑14 分钟前
SPI 子系统:从原理到实践雪雪爱冲浪20 分钟前
JavaScript 网络爬虫教程(2026)Data analyse45627 分钟前
rrweb回放事件资源加载顺序重放校验软件技术NINI28 分钟前
化妆品html+css+js 7页要吃这碗饭36 分钟前
JavaScript 网络爬虫教程(2026)lexiangqicheng43 分钟前
自定义表单跨端共用机制-PC设计APP渲染zhikouai1 小时前
AI辅助逆向工程链路:从二进制接入到等价性校验沙漏无语1 小时前
pnpm 安装@vue-office包的幽灵依赖问题解决霍格沃兹测试学院-小舟畅学1 小时前
Playwright MCP:AI Agent 驱动 Web 自动化测试的架构与执行机制Jmyd01231 小时前
VR全景线上展会制作流程技术解析:全景采集、点云重建与Web端渲染优化实战