// 函数节流 一段时间只能提交一次 const throttle = ((func, wait) => { console.log('12333333') let timer; // console.log("timer") // console.log(timer) return () => { // console.log("timer") // console.log(timer) if (timer) { console.log('停止') return; } timer = setTimeout(() => { // func(); console.log('执行一次') timer = null; }, 1000); }; })() //自调用形成封闭栈环境 闭包 throttle() throttle() throttle()
// // 函数防抖 高频点击提交按钮 // const debounce = (func, wait) => { // let timer; // return () => { // clearTimeout(timer); // timer = setTimeout(func, wait); // }; // };
参考地址:https://www.jianshu.com/p/f9f6b637fd6c