<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
相关推荐
谢亮_vipxieliang6 分钟前
JavaScript this 指向完全指南:调用位置决定一切Eiceblue22 分钟前
使用 Python 实现 Excel 行列转置(保留单元格样式)WeiXin_DZbishe24 分钟前
基于Node.js的马拉松赛事管理系统的设计与实现-计算机毕设 附源码【课程设计】90211西西学代码37 分钟前
像素和颜色之间的转换(Flutter)茶底世界之下1 小时前
羽毛颜色已经很鲜艳,还要继续加色吗?用 Awaking 试一次减饱和风早爽太1 小时前
Mac 搭建 Vue 开发环境Lank_M1 小时前
Word图片裁剪转HTML失效,转换器各漏读了哪一层青烟小生1 小时前
前端架构设计实战:从组件思维到系统思维时间海8711 小时前
对象键清洗之前,先拦住重名Amos_Web1 小时前
Rspack 源码解析(二十):Typed Hook 系统如何靠宏生成