<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
相关推荐
用户5508492902562 分钟前
前端本地存储怎么选:Cookie、localStorage、sessionStorage、IndexedDB 的取舍用户5508492902562 分钟前
Git 日常命令与分支协作流程:从提交到合并的实战梳理修炼的dance2 分钟前
同一段视频没降画质为什么就快了asong25 分钟前
从写代码到部署上线,Cloudflare 给 AI 配了一把新钥匙用户53723128824638 分钟前
你的 WGSL 从未被执行过——一个 secure context 静默陷阱的排查实录Daniel_1231 小时前
轻量级站点监控面板 LightPing 开源啦想风1 小时前
A/B 测试怎么做?独立站转化率优化的完整实操步骤lerhxx1 小时前
从 Markdown 到生成式 UI:AI 应用中的流式渲染实践迅猛龙办公室1 小时前
python实现简单进度条Rosanci1 小时前
Codex 下载与本地部署实战:从安装到运行全流程指南