<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
相关推荐
2501_933923256 分钟前
设计网页的时候加载不出来怎么办?认识常见的状态码only-lucky13 分钟前
QML深入学习五(Controls模块)程序员黑豆22 分钟前
鸿蒙应用开发之路由:Router 页面路由使用教程gyx_这个杀手不太冷静36 分钟前
Agent开发进阶指南(第 2 章):Agent 运行全流程拆解、上下文窗口、流式输出、记忆系统与 Function Call 实战anyup37 分钟前
像这种问题千万别自己动手,否则你可太看不起 AI 了hunterandroid1 小时前
[鸿蒙从零到一] HarmonyOS Web 组件与 JSBridge 通信实战:从页面加载到安全协议xingren1 小时前
「拍摄器」UI 特效 - 在 Winform/WPF/WinUI3/Avalonia/Web 的实现其美杰布-富贵-李1 小时前
第 5 篇:Object3D、Group 与场景树leslie1182 小时前
npm常用命令艾伦野鸽ggg2 小时前
axios 基本操作