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

相关推荐
对空六课11 小时前
支持注意力分析的热力图工具有哪些?
前端·数据库·数据分析
Csvn12 小时前
Vue3 响应式与编译:依赖收集如何升级为节点级靶向更新
前端
星栈13 小时前
pnpm 12 升级实测
前端·javascript
Sirens.13 小时前
Java并发锁详解:六类锁策略与 synchronized 底层原理
java·前端·算法
Ai-_Man14 小时前
您您这可以把Microsofat Copilot的多个会话比如说。左侧的多个会话一次性导出吗?不是单条会话里面的多次会对话。AI导出鸭
javascript·人工智能·ai·小程序·电脑·copilot
特创数字科技15 小时前
一个纯本地运行的图片处理工具:压缩 / 裁剪 / 九宫格 / 圆角 / 滤镜 / 拼图 / 水印,终生使用
前端
IT_陈寒15 小时前
Vue的v-if和v-for混用居然是个天坑
前端·人工智能·后端
天衍四九-16 小时前
【无标题】
前端·spring boot·mysql·nginx·docker
广州华水科技16 小时前
2026年单北斗GNSS变形监测系统推荐榜单,解锁GNSS位移监测新高度
前端
xcyxiner16 小时前
DicomViewer24 修复编译bug(window test 失败)
前端·qt