Scroll Event Running Slow - Is There A Lighter Way?
I have the following scroll event that on scroll measures where in the page the user is and updates the navigation styling according to which section they are in. The problem is th
Solution 1:
Use jQuery throttle / debounce by Ben Alman
$(window).scroll( $.throttle( 250, function(){...} ) );
http://benalman.com/code/projects/jquery-throttle-debounce/jquery.ba-throttle-debounce.js
Solution 2:
If you are looking to just delay the amount of calls, then this could work
var waitForFinalEvent = (function () {
var timers = {};
returnfunction (callback, ms, uniqueId) {
if (!uniqueId) {
uniqueId = "Don't call this twice without a uniqueId";
}
if (timers[uniqueId]) {
clearTimeout (timers[uniqueId]);
}
timers[uniqueId] = setTimeout(callback, ms);
};
})();
This is some code that I found on here actually, forgive me I don't have the url to link back to the original source. This will delay the call X amount of seconds after the scroll. Meaning, instead of making fifty bigillion calls in that time frame, it'll make one, which could only help.
The way you call it is like this:
waitForFinalEvent(function() {
//stuff to do
}, 500, "randomString");
Hope this helps!! Adjust the 500 to the amount of time you want to delay.
ORIGINAL POST: JavaScript/JQuery: $(window).resize how to fire AFTER the resize is completed?
Post a Comment for "Scroll Event Running Slow - Is There A Lighter Way?"