Skip to content

Instantly share code, notes, and snippets.

@yiyizym
Last active Dec 23, 2018
Embed
What would you like to do?
防抖节流
// 防抖
function debounce(fn, time){
let timer;
return function(){
clearTimeout(timer);
timer = setTimeout(function(){
fn();
}, time)
}
}
function fn(){console.log('fn')}
var nFn = debounce(fn, 1000);
// 节流
function throttle(fn, time){
let canRun = true;
return function(){
if(!canRun) return
canRun = false;
setTimeout(function(){
canRun = true;
fn();
}, time)
}
}
function fn(){console.log('fn')}
var nFn = throttle(fn, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment