/** * @description: * @param {*} fn * @param {*} time * @param {*} type 1表示立即执行,其他表示非立即执行 * @return {*} */ function fangdou(fn, time, type){ if(type == 1){ let timer = null; let canRun = true; return function(){ if(timer){ clearTimeout(timer); canRun = false; } timer = setTimeout(()=>{ timer = null; canRun = true; }, time) canRun && fn(...arguments) } }else{ let timer = null; return function(){ console.log("fff", arguments) if(timer){ clearTimeout(timer) } timer = setTimeout(()=>{ fn(...arguments) timer = null }, time) } } }
/** * @description: * @param {*} fn * @param {*} time * @param {*} type 1表示立即执行,其他表示非立即执行 * @return {*} */ function jieliu(fn, time, type){ let canRun = true; if(type == 1){ return function(){ if(canRun){ fn(...arguments); canRun = false; setTimeout(()=>{ canRun = true }, time) } } }else{ return function(){ if(canRun){ canRun = false; setTimeout(()=>{ fn(...arguments) canRun = true }, time) } } } }