Skip to content

Instantly share code, notes, and snippets.

@yiyizym
Last active December 23, 2018 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yiyizym/977d40b5aca499c1b52af7316987b5f3 to your computer and use it in GitHub Desktop.
Save yiyizym/977d40b5aca499c1b52af7316987b5f3 to your computer and use it in GitHub Desktop.
防抖节流
// 防抖
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