zoukankan      html  css  js  c++  java
  • 函数节流和防抖

    1.函数节流

      函数在时间间隔内只能被触发一次。

      函数节流有两种方式:

        (1)第一次执行:

    throttle(function,delay) {
        let timer = null;
        let first = true;
        let that = this;
        let args = arguments;
        return () => {
            if(first === false) {
                function.apply(this,function);
                return first = false;
            }
            if(timer) {
                return;
            }
            timer = setTimeout(() => {
    clearTimeout(timer);
    timer = null;
    function.apply(that,args); },delay) } }

        (2)第一次不执行,delay之后再执行:

    throttle(function,delay) {
        let timer = null;
        let that = this;
        let args = arguments;
        return () => {
            if(timer) {
                return;
            }
            timer = setTimeout(() => {
                clearTimeout(timer);
                timer = null;
                function.apply(that,args);
            },delay)
        }
    }

    2.函数防抖

      触发函数后一定时间后执行,但如果等待这段时间又被触发则重新开始计时。

    debounce(function,delay) {
        let timer = null;
        let that = this;
        let args = arguments;
        return () => {
            if(timer) {
                clearTimeout(timeout);
            }
            timer = setTimeout(() => {
                function.apply(that,args);
            },delay);
        }
    }
  • 相关阅读:
    C# 实现类库并调用
    C# pictureBox.Image获得图片的三种方法
    C# 指针使用总结
    C++ 怎样让函数返回数组
    C# 枚举与位枚举(Enum)
    Labview调用C#动态链接库dll
    C# partial 作用
    C# Internal关键字小结
    C# => 运算符
    C# 中 ??、 ?、 ?: 、?.、?[ ]
  • 原文地址:https://www.cnblogs.com/atao24/p/14794465.html
Copyright © 2011-2022 走看看