uniapp 常用定时器实现方式

无线循环计时器 setInterval

javaScript 复制代码
 //创建定时器  该定时器必须手动取消否则会造成泄漏一直存在
 let time = setInterval(() => {
					Array.from(this.list).forEach((item)=>{
						item.title +=1 
					})
				}, 2000)
 //取消定时器				
	clearInterval(time)
	
	
	//可以在页面销毁回调进行取消 注意let 标识局部变量 这边取消需要定义为 var 
	export default {beforeDestroy() {
			    clearInterval(this.time)
	    	}
		}

倒计时 setTimeOut 只执行一次

javaScript 复制代码
setTimeout(()=>{//到期后执行
					Array.from(this.list).forEach((item)=>{
											item.title +=1 
										})
				},2000)

示例

html 复制代码
<template>
	<view>
		<view>
		</view>
		<view class="line" v-for="(item,i) in list" :key="i" @click="itemClick(key)">
			<image class="imageStype" src="../../static/logo.png"></image>
			<text class="textHellow">{{item.title}}</text>
		</view>
	</view>
</template>
<script>
export default {
		data() {
			return {
				list: [{
					title: 0
				}, {
					title: 1
				}, {
					title: 2
				}]

			}
		},
		methods: {
			itemClick(ii) {
				// setInterval(() => {
				// 	Array.from(this.list).forEach((item)=>{
				// 		item.title +=1 
				// 	})
				// }, 2000)
				setTimeout(()=>{
					Array.from(this.list).forEach((item)=>{
											item.title +=1 
										})
				},2000)
			}
		}
	}
</script>

<style>
	.imageStype {
		width: 120rpx;
		height: 120rpx;
		margin-left: 40rpx;

	}

	.line {
		margin-top: 20rpx;
		display: flex;
		flex-direction: row;
		padding-top: 40rpx;
		padding-bottom: 40rpx;
		width: 670rpx;
		margin-left: 40rpx;
		border: 2rpx solid gray;
		border-radius: 16rpx;
		background-color: aliceblue;
		box-shadow: 8rpx 8rpx 4rpx 0 gray;
	}

	.textHellow {
		margin-left: 20rpx;
		font-weight: bold;
		font-size: 32rpx;
	}
</style>

大致效果 列表点击后过2秒 标题数值变化

相关推荐
默_笙1 小时前
🔥 让 AI 帮我写完整个 Next.js 博客:框架就是 AI 的"上下文 buff"
前端·javascript
牧艺2 小时前
登录页还在用渐变?我一口气做了 5 个能摸的背景动效:吹蒲公英、滴墨染水、点熔岩闷裂
前端·canvas·交互设计
陆枫Larry2 小时前
状态机简介
前端
陆枫Larry2 小时前
一次登录问题排查:接口没错,错的是旧业务逻辑
前端
小小小小宇2 小时前
大模型打分与采样原理,以及 Pi Agent 核心原理
前端
一位正在转型AI全栈的前端工程师3 小时前
AI 全栈学习之旅 -Week 5:RAG 知识库问答系统:从零到生产级部署的全栈实践总结
前端·python
光影少年3 小时前
react navite 安卓iOS 打包、签名、环境区分
前端·react native·react.js
coderCN3 小时前
Nodejs 第三十四章 数据库(表达式和函数、子查询和连表)
前端·node.js
ssshooter4 小时前
AI 时代你不能不知道的 git worktree
前端·后端·面试
观测云4 小时前
AI时代的用户访问监测:观测云带你身临其境体验用户与前端UI交互旅程
前端·可观测性·观测云·rum