Created
August 5, 2017 11:23
-
-
Save chekit/ece8db0b170a1be30d328c22a52d3648 to your computer and use it in GitHub Desktop.
Debounce and Throttle methods
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
/* | |
Debounce | |
*/ | |
export function debounce(fn, time) { | |
let timeout = null; | |
return function () { | |
let context = this; | |
let args = arguments; | |
clearTimeout(timeout); | |
timeout = setTimeout(function () { | |
timeout = null; | |
fn.apply(context, args); | |
}, time); | |
} | |
} | |
/* | |
Throttle | |
*/ | |
export function throttle(fn, time) { | |
let timeout = null; | |
return function () { | |
let context = this; | |
let args = arguments; | |
if (!timeout) { | |
fn.apply(context, args); | |
timeout = setTimeout(function () { | |
timeout = null; | |
}, time); | |
} | |
} | |
} | |
// http://jsbin.com/zinarem/edit?js,console,output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment