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

相关推荐
Felven2 小时前
C. Make Them Equal
c语言·开发语言·前端
智能收银研究所5 小时前
收银系统选型对比:美团、客如云、拉卡拉、收钱吧各适合什么门店
java·前端·数据库
滑动变滚动的蜗牛8 小时前
我发布了自己的个人小程序(诉述洞)
前端·ai编程·全栈
ZYH_06019 小时前
Shell 第一阶段笔记:变量、引号与变量截取
前端·chrome·笔记
YIAN10 小时前
流式 TTS 和普通TTS到底有什么区别?一个文档教会你!
前端·websocket·node.js
时间的情敌10 小时前
Three.js GLTF 阀门 / 泵体模型加载、材质修改、状态联动(正常 / 故障 / 告警变色)
开发语言·javascript·three.js·材质
掘金安东尼11 小时前
智能体包办工作时,批判性思维怎么办(Addy Osmani 全文转译)
前端·程序员·agent
SameX13 小时前
HarmonyOS ArkTS Canvas 画环形进度条 animateTo + @Watch 驱动重绘 120 行实现
前端
bai27313 小时前
日志里的 [Object] 不是空对象:console.log 默认只展开两层,console.dir 也一样
javascript·后端
欧雷殿13 小时前
我的 AI 外包实战:用数字员工做项目的一人公司
前端·ai编程·团队管理