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

相关推荐
逝缘~12 分钟前
css百分比布局中height:100%不起作用
前端·css·小程序·html·js
Passion不晚14 分钟前
打造古风炫酷个人网页:用HTML和CSS3传递笔墨韵味
前端·html·css3
浏览器爱好者1 小时前
如何在Chrome中使用HTML构建交互式网页
前端·chrome·html
gs801401 小时前
安装node 报错需要:glibc >= 2.28
linux·服务器·前端·node.js
小汤猿人类4 小时前
uniapp媒体
uni-app·媒体
Random_index4 小时前
#名词区别篇:npx pnpm npm yarn区别
前端·npm
B.-4 小时前
Remix 学习 - 路由模块(Route Module)
前端·javascript·学习·react·web
陈大爷(有低保)4 小时前
JS基础语法
开发语言·javascript·ecmascript
不修×蝙蝠5 小时前
Javascript应用(TodoList表格)
前端·javascript·css·html
加勒比海涛5 小时前
ElementUI 布局——行与列的灵活运用
前端·javascript·elementui