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

相关推荐
stereohomology4 分钟前
已进入初赛提交Demo的两个项目:(2)单独html钢琴
前端·html·why不coding
源图客11 分钟前
Claude Code基础使用
服务器·前端·数据库
asdzx6727 分钟前
在 React 中轻松实现 PDF 转 Word:纯前端 WASM 方案实战
前端·react.js·pdf
爱勇宝9 小时前
第 1 章:别把“需求文档”当成真正的需求
前端·后端·程序员
梨子同志10 小时前
常用命令
前端
梨子同志10 小时前
React 状态管理
前端
Dxy123931021610 小时前
MySQL索引完整教程:创建、查看、修改、删除与日常管理
前端·javascript·mysql
剪刀石头布啊11 小时前
js能被遍历的集合有哪些特征,为何能被遍历
前端
剪刀石头布啊11 小时前
js解构
前端
剪刀石头布啊11 小时前
防抖功能的逐步递进
前端