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

相关推荐
会周易的程序员6 分钟前
js-shm: 高性能 Node.js 共享内存模块
开发语言·javascript·c++·node.js·共享内存·shm
闲猫9 分钟前
Agent工程实践:从WorkFlow到多Agent协作的落地笔记
java·服务器·前端
带娃的IT创业者23 分钟前
重新定义前端构建速度:深度解析 SWC 如何用 Rust 颠覆 JavaScript 工具链
前端·javascript·rust·前端构建·swc
happy_0x3f32 分钟前
前端应用的离线暂停更新策略
开发语言·前端·php
m0_547486661 小时前
《Vue.js + uni-app全栈开发从入门到实践 》全套PPT课件2026版
vue.js·uni-app
你驴我1 小时前
WhatsApp 消息撤回与编辑的幂等性设计实践
java·服务器·前端·后端·python
新中地GIS开发老师2 小时前
零基础WebGIS开发入门 | GeoJSON数据持久化
前端·javascript·gis·webgis·三维gis开发
blns_yxl2 小时前
正则驱动实时表单验证(HTML+CSS+JS+正则)
javascript·css·html
sunywz2 小时前
【c#】 Web Deploy一键发布,IIS部署全流程
开发语言·前端·c#
宁&沉沦2 小时前
Chrome 扩展 Manifest 字段版本支持一览(全量)
前端·后端·编辑器