<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
相关推荐
Mh2 小时前
基于踩过的坑,浅聊一下vh、svh、lvh、dvh刘小灰3 小时前
返璞归真,我开始更新博客了圆心角4 小时前
Agent 上下文原理:为什么“记住”比“推理”更难杜子不疼.7 小时前
从 Apache HTTP Server 到 CivetWeb:在鸿蒙 PC 的 3.1 MiB HAP 里嵌入 Web 服务器kyriewen8 小时前
AI 生成率进了 KPI,最慌的是写代码最快的人SoaringHeart8 小时前
Flutter组件封装:聊天气包图片组件NChatBubble狗头大军之江苏分军9 小时前
三个月的电商心得勤恳的马龙11 小时前
给 1088 个工具页批量注入 FAQ 和结构化数据:一次幂等的 SEO 工程htzyl20612 小时前
javaScript中,让你全面了解动态import()的四个真实用途,及与静态import的区别OpsEye12 小时前
新手别混淆!RPM 和 TPM,大模型调用的两个限流密码