# throttle & debounce节流防抖

# 平台差异说明

App(vue) App(nvue) H5 小程序

# 何谓节流和防抖?

  • 节流
    节流的意思是,规定时间内,只触发一次。比如我们设定500ms,在这个时间内,无论点击按钮多少次,它都只会触发一次。具体场景可以是抢购时候,由于有无数人 快速点击按钮,如果每次点击都发送请求,就会给服务器造成巨大的压力,但是我们进行节流后,就会大大减少请求的次数。

  • 防抖
    防抖的意思是,在连续的操作中,无论进行了多长时间,只有某一次的操作后在指定的时间内没有再操作,这一次才被判定有效。具体场景可以搜索框输入关键字过程中实时 请求服务器匹配搜索结果,如果不进行处理,那么就是输入框内容一直变化,导致一直发送请求。如果进行防抖处理,结果就是当我们输入内容完成后,一定时间(比如500ms)没有再 输入内容,这时再触发请求。

结合以上两种情况,回到我们最实际的场景,比如防止表单提交按钮被多次触发,我们应该选择使用节流而不是防抖方案。

温馨提示

uview-plus内置的按钮组件up-button内部已做节流处理(1.5.8版本),无需外部再做节流处理。配置throttle-time参数,可以设置节流的时间,详见Button 按钮

# 节流

# throttle(func, wait = 500, immediate = true)

规定时间内,只触发一次,可以通过设置immediate来决定触发的时机在这个时间的开始,还是结束的时候执行。

  • func <Function> 触发回调执行的函数
  • wait <Number> 时间间隔,单位ms
  • immediate <Boolean> 在开始还是结束处触发,比如设置wait为1000ms,如果在一秒内进行了5次操作,只触发一次,如果immediatetrue,那么就会在第一次操作 触发回调,如果为false,就会在第5次操作触发回调。
<template>
    <view class="">
        <view class="throttle" @tap="throttle(btnAClick, 500)">
            露从今夜白
        </view>
        <view class="throttle" @tap="btnBClick">
            月是故乡明
        </view>
    </view>
</template>

<script setup>
import { throttle } from '@/uni_modules/uview-plus';

const btnAClick = () => {
    console.log('btnClick');
};

const btnBClick = () => {
    // 此处用法为在js中调用,需要写throttle()
    throttle(toNext, 500);
};

const toNext = () => {
    console.log('btnBClick');
};
</script>

<style lang="scss">
    .throttle {
        margin-top: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100rpx;
        border: 1px solid $up-type-primary;
    }
</style>
<template>
    <view class="">
        <!-- 此处用法为在模板中使用,无需写this,直接$u.throttle()即可 -->
        <view class="throttle" @tap="throttle(btnAClick, 500)">
            露从今夜白
        </view>
        <view class="throttle" @tap="btnBClick">
            月是故乡明
        </view>
    </view>
</template>

<script>
import { throttle } from '@/uni_modules/uview-plus';
export default {
    methods: {
        throttle,
        btnAClick() {
            console.log('btnClick');
        },
        btnBClick() {
            // 此处用法为在js中调用,需要写throttle()
            throttle(this.toNext, 500);
        },
        toNext() {
            console.log('btnBClick');
        }
    }
}
</script>

<style lang="scss">
    .throttle {
        margin-top: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100rpx;
        border: 1px solid $up-type-primary;
    }
</style>

# 防抖

# debounce(func, wait = 500, immediate = false)

在连续的操作中,无论进行了多长时间,只有某一次的操作后在指定的时间内没有再操作,这一次才被判定有效

  • func <Function> 触发回调执行的函数
  • wait <Number> 时间间隔,单位ms
  • immediate <Number> 在开始还是结束处触发,如非特殊情况,一般默认为false即可
<template>
    <view class="">
        <view class="debounce" @tap="debounce(btnAClick, 500)">
            露从今夜白
        </view>
        <view class="debounce" @tap="btnBClick">
            月是故乡明
        </view>
    </view>
</template>

<script setup>
import { debounce } from '@/uni_modules/uview-plus';

const btnAClick = () => {
    console.log('btnClick');
};

const btnBClick = () => {
    // 此处用法为在js中调用,需要写debounce()
    debounce(toNext, 500);
};

const toNext = () => {
    console.log('btnBClick');
};
</script>

<style lang="scss">
    .debounce {
        margin-top: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100rpx;
        border: 1px solid $up-type-primary;
    }
</style>
<template>
    <view class="">
        <!-- 此处用法为在模板中使用,无需写this,直接$u.debounce()即可 -->
        <view class="debounce" @tap="debounce(btnAClick, 500)">
            露从今夜白
        </view>
        <view class="debounce" @tap="btnBClick">
            月是故乡明
        </view>
    </view>
</template>

<script>
import { debounce } from '@/uni_modules/uview-plus';
export default {
    methods: {
        debounce,
        btnAClick() {
            console.log('btnClick');
        },
        btnBClick() {
            // 此处用法为在js中调用,需要写uni.$u.debounce()
            debounce(this.toNext, 500)
        },
        toNext() {
            console.log('btnBClick');
        }
    }
}
</script>

<style lang="scss">
    .debounce {
        margin-top: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100rpx;
        border: 1px solid $up-type-primary;
    }
</style>