<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中实现计时器效果
蜕变菜鸟2024-08-27 17:46
相关推荐
十有八七5 小时前
手搓 Agent(一):先有大脑——大模型只会做一件事锋行天下5 小时前
「踩坑实录」给 LangGraph Agent 接两个 FastMCP,我被 langchain 偷偷改了工具名跳跳的小星星6 小时前
自研组件】Vue2 + ElementUI 通用列表页封装 wkjp-list-page,一套配置搞定搜索、动态表格、分页、增删改tryCbest6 小时前
Vue3—— 第 18 篇:Vue 2 到 Vue 3 迁移指南——升级避坑全记录保利威视频SaaS小张6 小时前
企业直播接入笔记 12|企业内训直播怎么做签到和学时统计,防挂机这件事比想象中难bysking7 小时前
GitHub Actions 快速入门+场景实践案例广州华水科技7 小时前
单北斗GNSS变形监测系统在大坝安全维护中的重要性anyup7 小时前
自带 uni-app x 页面级能力,uView Pro X 8 个开箱即用的工具函数涛涛ing7 小时前
别拿 Chrome 跑通了就提测:把这段 AI 写的代码发到 Safari 上,我差点丢了工作墨林陌7 小时前
用 AI 给娃做了一个识物游戏:一份 2.3MB 的单文件 HTML,126 张卡片、三种玩法,手机平板打开就能玩