Last active
December 23, 2018 06:15
-
-
Save yiyizym/977d40b5aca499c1b52af7316987b5f3 to your computer and use it in GitHub Desktop.
防抖节流
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 防抖 | |
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 节流 | |
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