<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
相关推荐
遂心_5 分钟前
JavaScript 函数参数传递机制:一道经典面试题解析小徐_233315 分钟前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!RoyLin18 分钟前
TypeScript设计模式:适配器模式遂心_40 分钟前
深入理解 React Hook:useEffect 完全指南Moonbit41 分钟前
MoonBit 正式加入 WebAssembly Component Model 官方文档 !龙在天1 小时前
ts中的函数重载卓伊凡1 小时前
非常经典的Android开发问题-mipmap图标目录和drawable图标目录的区别和适用场景实战举例-优雅草卓伊凡前端Hardy1 小时前
HTML&CSS: 谁懂啊!用代码 “擦去”图片雾气前端Hardy1 小时前
HTML&CSS:好精致的导航栏天下无贼1 小时前
【手写组件】 Vue3 + Uniapp 手写一个高颜值日历组件(含跨月补全+今日高亮+选中状态)