-
-
Save osheinonen/0b137ffc51f61935b4df1ceb2f481ec0 to your computer and use it in GitHub Desktop.
Revisions
-
beaucharman revised this gist
Feb 1, 2017 . No changes.There are no files selected for viewing
-
beaucharman revised this gist
Jul 11, 2016 . 1 changed file with 21 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,3 @@ function debounce(callback, wait, context = this) { let timeout = null let callbackArgs = null @@ -27,3 +10,24 @@ function debounce(callback, wait, context = this) { timeout = setTimeout(later, wait) } } /** * Normal event * event | | | * time ---------------- * callback | | | * * Call log only when it's been 100ms since the last sroll * scroll | | | * time ---------------- * callback | | * |100| |100| */ /* usage const handleScroll = debounce((e) => { console.log('Window scrolled.') }, 100) window.addEventListener('scroll', handleScroll) */ -
beaucharman renamed this gist
Jul 11, 2016 . 1 changed file with 1 addition and 30 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ // Normal event // event | | | // time ---------------- // callback | | | @@ -26,33 +27,3 @@ function debounce(callback, wait, context = this) { timeout = setTimeout(later, wait) } } -
beaucharman revised this gist
Jul 11, 2016 . 1 changed file with 31 additions and 29 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,56 +1,58 @@ // event | | | // time ---------------- // callback | | | // Call log only when it's been 100ms since the last sroll // scroll | | | // time ---------------- // callback | | // |100| |100| const handleScroll = debounce((e) => { console.log('Window scrolled.') }, 100) window.addEventListener('scroll', handleScroll) function debounce(callback, wait, context = this) { let timeout = null let callbackArgs = null const later = () => callback.apply(context, callbackArgs) return function() { callbackArgs = arguments clearTimeout(timeout) timeout = setTimeout(later, wait) } } // Call search at most once per 300ms while keydown // keydown | | | | // time ----------------- // search | | // |300| |300| const handleKeydown = debounce((e) => { console.log(e.target.value) }, 300) input.addEventListener('keydown', handleKeydown) function throttle(callback, wait, context = this) { let timeout = null let callbackArgs = null const later = () => { callback.apply(context, callbackArgs) timeout = null } return function() { if (!timeout) { callbackArgs = arguments timeout = setTimeout(later, wait) } } } -
beaucharman created this gist
Jun 4, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ // input.addEventListener('keydown', search); // keydown | | | // time ---------------- // search | | | input.addEventListener('keydown', debounce(myObject.search, 100, myObject)); // Call search only when it's been 100ms since the last keydown // keydown | | | // time ---------------- // search | | // |100| |100| function debounce(callback, duration, context) { var timeout = null var callbackArgs = null var later = function() { callback.apply(context, callbackArgs) } return function() { callbackArgs = arguments; clearTimeout(timeout) timeout = setTimeout(later, duration) } } // input.addEventListener('keydown', throttle(search, 100)); // Call search at most once per 100ms // keydown | | | | // time ----------------- // search | | // |100| |100| function throttle(callback, duration, context) { var timeout = null var callbackArgs = null var later = function() { callback.apply(context, callbackArgs) timeout = null } return function() { if (!timeout) { callbackArgs = arguments; timeout = setTimeout(later, duration) } } }