# debounce(func, wait = 500, immediate = true) 事件被触发后,等待一段时间(延迟时间),如果在这段时间内没有再次触发该事件,则执行函数;如果在这段时间内再次触发了该事件,则重新计时。
支持平台
| 安卓 | ios | web | 微信小程序 |
|---|---|---|---|
| √ | √ | √ | √ |
组件方式使用(推荐)
组件内置防抖功能,通过 :debounce 和 :tdTime 属性控制:
html
<template>
<view>
<t-button type="primary"
:debounce="true"
:tdTime="1000"
@click="onClick">点击触发</t-button>
</view>
</template>
<script setup>
function onClick() {
uni.showToast({
title: '事件被执行'
})
}
</script>API方式使用
javascript
import { TuiDebounce } from '@/uni_modules/tui-common'
const deb = new TuiDebounce()
deb.debounce(() => {
// 执行逻辑
}, 500)