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

相关推荐
Slice_cy几秒前
Mint 自研框架设计与实现:从重复开发走向配置驱动(三)
前端·后端·架构
申君健2486418277202几秒前
WebGPU 内部运行原理:Format、Texture 与 GPU 侧到底在干什么
javascript
JoyT12 分钟前
Nuxt 3的核心设计与静态官网应用
前端·javascript·vue.js
程序员黑豆13 分钟前
鸿蒙应用开发之@State 装饰器详解:从基本类型到 @Observed/@ObjectLink/@Track 嵌套监听
前端·harmonyos
杉氧15 分钟前
Flutter 像素级还原实战:用 CustomPaint 与 Bezier 曲线手绘精致图针
android·前端·flutter
金融小白数据分析之路2 小时前
绍兴市镇街echarts 制作
前端·数据库·echarts
Slice_cy2 小时前
Mint 自研框架设计与实现:从重复开发走向配置驱动(二)
前端·后端·架构
greenbbLV2 小时前
积分兑换商城供应商挑选:避坑要点与靠谱选择解析
前端·小程序
张元清2 小时前
React useDropZone Hook:搭建一个文件拖放区(2026)
javascript·react.js
a1117763 小时前
3D 建筑编辑器 Pascal Editor THreeJS 开源
前端·3d·html