I'm playing around with some javascript and JQuery. I'm trying to do something specific each time the user scrolls. It works fine when I use a mouse with a scollwheel. But when I use the trackpad on my laptop it goes incredibly fast. I think it's because it executes the mousewheel function many times when scrolling. So I want to limit how often you can execute the function. Like with a 500ms delay between each call if the user keeps scrolling. It shouldn't delay the functions itself but run immediately and then wait 500ms before it can be executed again. Hope you understand.
Here's my code
$(window).bind('mousewheel', function(event) { // My mousewheel function.
if (event.originalEvent.wheelDelta >= 0) {
scroll(-1);
} else {
scroll(1);
}
});
/* Functions */
function scroll(dir) {
if (dir == -1) {
if (current > 0) {
current--;
}
} else {
if (current < list.size() - 1) {
current++;
}
}
var number = 100 * current;
var value = "translateY(-" + number + "vh)";
main.css("transform", value);
for (var i = 0; i < list.size(); i++) {
$('#nav li:nth-child(' + (i + 1) + ')').removeClass('active');
}
$('#nav li:nth-child(' + (current + 1) + ')').addClass('active');
}