<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
相关推荐
zz58838 分钟前
面试官问"JS的类型"时,到底想听到什么?gjwjuejin41 分钟前
全埋点技术方案深度剖析:从事件代理到无痕采集的完整实现十有八七1 小时前
AI 开发,本质是一场文档的生命周期管理Hyyy1 小时前
普通前端自救记录——第0周前端若水2 小时前
在 Vue 2 与 Vue 3 中使用 markdown-it-vue 渲染 Markdown 和数学公式之歆2 小时前
DAY_10 JavaScript 深度解析:原型链 · 引用类型 · 内置对象 · 数组方法全攻略(下)行星飞行2 小时前
从 cursor 、 Claude code 迁移到 codex,30 分钟快速上手 codex 常用技巧__log2 小时前
ComfyUI 集成技术方案分析报告Pu_Nine_92 小时前
前端埋点从入门到企业实践:手写一个Demo + 主流方案对比ZC跨境爬虫3 小时前
跟着 MDN 学 HTML day_56:(HTML 表格基础完全指南)