<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
相关推荐
我叫蒙奇3 分钟前
JS中的隐式类型转换他们叫我秃子4 分钟前
前端开发转 Go 全栈(四):代码写在前面,却要最后执行?我终于搞懂了 defer生戎马 平安京策24 分钟前
只学一点点:我的技术学习策略大鹏说大话37 分钟前
HTML5 地理定位 Geolocation:获取用户位置的“红线”与最佳实践Csvn1 小时前
content-visibility: auto —— 让浏览器跳过离屏渲染的性能黑科技小鹰信息技术服务部1 小时前
Edge安装包MicrosoftEdgeSetup.exe无法运行,点击没反应webkubor2 小时前
一次前端生产白屏复盘:旧 HTML、Hash 资源和 MIME type text/html咩咩啃树皮2 小时前
第63篇【终极整合巨作】从零手写原生中后台管理系统|整合62篇全部前端知识点|从头到尾完整架构+全功能逻辑得物技术3 小时前
实战从零开始构建一个Coding Agent:Violin |得物技术Highcharts3 小时前
用Highcharts如何动态向一个序列添加点-示列讲解