在Uni-app中实现计时器效果

复制代码
<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中实现计时器效果_uniapp 计时器-CSDN博客

相关推荐
问心无愧05135 小时前
ctf show web入门160 161
前端·笔记
李小白665 小时前
第四天-WEB服务器基本原理,IIS服务
运维·服务器·前端
humcomm6 小时前
AI编程时代新前端职位
前端·ai编程
好家伙VCC6 小时前
Web Components主题热切换方案揭秘
java·前端
甲维斯7 小时前
Kimi版超级玛丽效果“惊人”,配额不足5厘米!
前端·人工智能
hboot7 小时前
AI工程师第一课 - Python
前端·后端·python
凉菜凉凉7 小时前
AI时代,被抛弃的前端
前端·ai
console.log('npc')7 小时前
AI前端工程与生成式UI学习路线
前端·人工智能·ui
huangdong_7 小时前
淘宝商品SKU图自动分类技术深度解析:从DOM解析到智能归档
开发语言·javascript·ecmascript
梦曦i7 小时前
uni-router v1.1.1发布:守卫超时保护+路由监听
前端·uni-app