您当前的位置: 首页 >  Java

暂无认证

  • 0浏览

    0关注

    92582博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

JavaScript之mj-debounce-throttle插件、封装、防抖、节流、define、global、clearTimeout、setTimeout、self、module、exports

发布时间:2022-06-10 19:53:41 ,浏览量:0

目录
  • 1、下载安装命令
  • 2、源码
1、下载安装命令
npm install mj-debounce-throttle
npm install mj-debounce-throttle --save
npm install mj-debounce-throttle --save-dev

下载安装后,包文件夹的.md文件有使用说明。

2、源码
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.debounceThrottle = factory()); }(this, function () { 'use strict'; return { debounce: (fn, delay = 1000) => { let timer = null; return function () { let context = this, args = arguments; if (timer) clearTimeout(timer); timer = setTimeout(function () { fn.apply(context, args); }, delay); }; }, throttle: (fn, wait = 1500) => { let inThrottle, lastFn; return function () { let context = this, args = arguments; if (!inThrottle) { fn.apply(context, args); inThrottle = true; } else { clearTimeout(lastFn); lastFn = setTimeout(function () { inThrottle = false; }, wait); } }; } }; })); 
关注
打赏
1653961664
查看更多评论
立即登录/注册

微信扫码登录

0.3673s