在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博客

相关推荐
程序员大阳21 分钟前
HTML+CSS+JS对话式实战(5)-活动管理页之查看报名名单的实现
javascript·css·html·查看报名名单
曦樂~37 分钟前
【Web Development】02 CSS
前端·css
风骏时光牛马1 小时前
后端服务接口开发与数据逻辑处理
前端
东鹏特饮1 小时前
经典打砖块(Breakout)游戏
前端·css·css3
IT_陈寒1 小时前
Vite的HMR把我整不会了:一个生产环境的热更新坑
前端·人工智能·后端
Mh1 小时前
如何使用双缓冲优化视频切换
前端
risenav1 小时前
低头蓄力,抬头起跳:《萤虫跳跳》的 Rokid AIUI 开发实战
前端
ZhengEnCi2 小时前
如何看待 Fable 5.2 用纯代码生成 1.4MB 视觉作品:光追、水面、倒影、火光的程序化生成原理
前端
再吃一根胡萝卜2 小时前
DSH 插件的 Host 与 Client:为什么「只加载了 Host」,以及要不要自己写 Client 加载器
前端
parade岁月3 小时前
AI 写的前端代码为什么总像 5 年前的?Chrome 官方出手了
前端