<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 分钟前
7-10 WPS JS宏 对象使用实例7--拆分单表到多工作簿下的多表C_心欲无痕14 分钟前
不点击鼠标也能通过MouseEvent实现点击事件鲨莎分不晴40 分钟前
【实战】老项目焕发新生:从 Webpack 平滑迁移到 Vite 避坑全记录web小白成长日记1 小时前
从零起步,用TypeScript写一个Todo App:踩坑与收获分享圣心1 小时前
Gemini 模型 介绍huangyiyi666662 小时前
前端-远程多选搜索框不能反显的问题解决敲敲了个代码2 小时前
让 Vant 弹出层适配 Uniapp Webview 返回键AOwhisky2 小时前
Ansible管理变量和事实(管理变量部分) & 部署文件到受管主机摘星编程2 小时前
React Native for OpenHarmony 实战:Accessibility 辅助功能详解小二·2 小时前
Python Web 开发进阶实战:绿色软件工程 —— 构建低能耗、低碳排的可持续应用