Last active
August 28, 2016 19:13
-
-
Save groz/6255dac134838a9c79f3e2ef3a768b35 to your computer and use it in GitHub Desktop.
Debounce afisha daily
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 characters
| // this is the lag source (decompiled code) | |
| addEventScroll: function(e, t) { | |
| var n = this; | |
| e.attachEvent ? e.attachEvent("onscroll", function() { | |
| t.call(n) | |
| }) : e.addEventListener("scroll", function() { | |
| t.call(n) | |
| }) | |
| }, | |
| // FIX: use http://underscorejs.org/#debounce or directly copy debounce function | |
| if (typeof _ === "undefined") { | |
| _ = {} | |
| } | |
| _.now = Date.now || function() { | |
| return new Date().getTime(); | |
| }; | |
| _.debounce = function(func, wait, immediate) { | |
| var timeout, args, context, timestamp, result; | |
| var later = function() { | |
| var last = _.now() - timestamp; | |
| if (last < wait && last >= 0) { | |
| timeout = setTimeout(later, wait - last); | |
| } else { | |
| timeout = null; | |
| if (!immediate) { | |
| result = func.apply(context, args); | |
| if (!timeout) context = args = null; | |
| } | |
| } | |
| }; | |
| return function() { | |
| context = this; | |
| args = arguments; | |
| timestamp = _.now(); | |
| var callNow = immediate && !timeout; | |
| if (!timeout) timeout = setTimeout(later, wait); | |
| if (callNow) { | |
| result = func.apply(context, args); | |
| context = args = null; | |
| } | |
| return result; | |
| }; | |
| } | |
| // replace call being attached with debounced call | |
| addEventScroll: function(e, t) { | |
| var intervalInMs = 100; | |
| var n = this; | |
| e.attachEvent ? e.attachEvent("onscroll", _.debounce(function() { | |
| t.call(n) | |
| }, intervalInMs)) : e.addEventListener("scroll", _.debounce(function() { | |
| t.call(n) | |
| }, intervalInMs)) | |
| }, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment