Delaying Javascript Event Execution
Monday, October 13, 2008·Share·CommentsIf you're looking to execute javascript code whenever someone finishes (or stops temporary) scrolling, moving the mouse, or resizing the page, you may find the following code segment useful.
var onFooEndFunc = function() { var throttleTimer; return function(event) { throttleTimer = (!!throttleTimer) ? window.clearTimeout(throttleTimer) : null; throttleTimer = window.setTimeout(function() { // Your code here }, 50); }; }();As an example, let's say you have an element with ID "foobar" and you want to perform some action on scroll, but only when the user stops scrolling. You can do this by creating the function above and assigning it to the element's onscroll event handler:
document.getElementById("foobar").onscroll = onFooEndFunc;