zoukankan      html  css  js  c++  java
  • JS请求节流

    少废话,撸代码。欧耶!

    • 1、节流器
    // 对函数进行 节流
    function throttle (fn, interval = 500) {
      let timer = null;
      let firstTime = true;
    
      return function () {
        let args = arguments;
        if (firstTime) {
          // 第一次加载
          fn.apply(this, args);
          return firstTime = false;
        }
        if (timer) {
          // 定时器正在执行中,跳过
          return;
        }
        timer = setTimeout(() => {
          clearTimeout(timer);
          timer = null;
          fn.apply(this, args);
        }, interval);
      };
    }
    
    • 2、初始化节流器
        const throttleFunc=throttle(function (field) {
            var loadIndex = layer.load(1);
            $.ajax({
                type: 'POST',
                url: '/cms/teachers/saveTeacherInfo',
                async: false,
                data: data.field,
                success: function (result) {
                    if (result.status==1) {
                        layer.close(loadIndex);
                        layer.msg('保存成功');
                        canJump = 1;
                        if (!isJump) {
                            $('.cancel').trigger('click');
                        }
                    } else {
                        $('button[lay-filter="saveTeacherInfo"]').attr("disabled", false).removeClass("layui-disabled");
    
                        layer.close(loadIndex);
                        layer.msg(result.msg);
                    }
                }
            });
            return false;
        },1000);
    
    
    • 3、使用节流器
    throttleFunc(data.field);
    
  • 相关阅读:
    JDK动态代理
    回顾反射机制Method
    静态代理和动态代理
    使用jQuery实现ajax请求
    ajax函数
    事件 on
    函数2
    pytest-mock 调试实例
    Linux自启动tomcat
    第一次做性能测试
  • 原文地址:https://www.cnblogs.com/chenyangqit/p/11557675.html
Copyright © 2011-2022 走看看