When I bind function to scroll and scroll mousewheel once, the function runs seven or eight times. I want it to run only once when I scroll the mousewheel once, is this possible? I use this code:
$(document).ready(function () {
$(window).bind('scroll', function() {
alert("Scroll.");
}
});
});
You can use throttle function from jQuery throttle debounce plugin.
just a variation of Aruns answer:
Similar approach,just that the event get's fired immediately and is then prevented to be handled again for an interval of time. So that you wouldn't have to wait before handling the event.
If by "once" you mean, once-per-page-load, you could unbind the event in the listener with the
off()
method. http://api.jquery.com/off/There's also the possibility of namespacing events like:
scroll.namespace
is still the same event, but you can unbind it specifically.Try
Demo: Fiddle
If you want to detect scrolling only once, use this:
but if you want to detect every scroll event, try something like the answer 4