<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
相关推荐
雁鸣零落12 分钟前
如何在 Chrome 中查看其他浏览器的书签?书签空间订阅与侧边栏只读切换指南hpoenixf43 分钟前
一天上线 + 零返工:我如何给复杂前端需求建立“安全感”广州华水科技1 小时前
单北斗GNSS变形监测系统在水利工程安全保障中的应用与优势分析yqcoder2 小时前
CSS 外边距重叠(Margin Collapsing):现象、原理与完美解决方案山楂树の2 小时前
图像标注大坑:img图片 + Canvas 叠加标注,同步放大后标注位置偏移、对不齐?详解修复方案及亚像素处理原理本山德彪2 小时前
我做了一个拼豆图纸生成器,把照片秒变图纸DTrader3 小时前
用TS无法实盘量化? - 实盘均线策略进击的夸父3 小时前
vfojs:Vue 超集架构,外壳React灵魂Vue编程老船长3 小时前
解决不同项目需要不同 Node.js 版本的问题Wect3 小时前
LeetCode 5. 最长回文子串:DP + 中心扩展