Scroll Function Firing Multiple Times Instead of Once

I am trying to create a website that automatically scrolls to each section upon a single scroll action. This means that the code has to check if the page is scrolled up or scrolled down. I believe the code below solves my problem but the scroll action is fired more than once while the page is scrolling. You will see that the first alert in the if statement reaches 5 instead of the desired 1. Any help on the matter would be highly appreciated.

[Note] I am using the velocity.js library to scroll to each section within the container.

var page = $("#content-container");
var home = $("#home-layer-bottom");
var musicians = $("#musicians");
var athletes = $("#athletes");
var politics = $("#politics");
var bio = $("#politics");
var pages = [ home,musicians,athletes,politics,bio ];

var pageCur = 0;

var lastScrollTop = 0;

var st = $(this).scrollTop();
var pageNex = pageCur + 1;

if (st > lastScrollTop){
pages[pageNex].velocity("scroll", { container: $("#content-container") });
} else {
pages[pageCur-1].velocity("scroll", { container: $("#content-container") });
lastScrollTop = st;
pageCur = pageNex;


The scroll event (as well as the resize event) fire multiple times as a user does this. To help this, the best practice is to debounce. However, I've never gotten that to work. Instead, I use a global boolean to check if the function has fired.

var scrolled = false;
page.on('scroll', function(){
        scrolled = true;
        //do stuff that should take a while...
        scrolled = false;

This worked for me!

var ScrollDebounce = true;
function () {
if (ScrollDebounce) {
ScrollDebounce = false;

//do stuff    

setTimeout(function () { ScrollDebounce = true; }, 500);

Need Your Help

Where should store refetch action be made in Flux app after authentication?

javascript ajax reactjs flux

Suppose I have Messages, LoginForm components, MessageStore and UserStore (for keeping logged-in user info).

Showing status of current request by AJAX

ruby-on-rails ruby

I'm trying to develop an application which modifies a couple of tasks of the famous Online-TODO List RememberTheMilk ( using the REST API.