Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Scroll listener getting fired on initial page load

Hi,
I have the following js code :
 var firstScroll = true;
    $(window).scroll(scrollHandler);
    function scrollHandler() {
        if (firstScroll) {
            firstScroll = false;
            return;
        }
        if ($(window).scrollTop() === $(document).height() - $(window).height()) {
            $(window).off("scroll", scrollHandler);
            var data = $("#data").data();
            var origin = window.location.origin;
            var parameters = {
                "sha": data.lastSha
            };
            var url = '/github/get-commits/?' + encodeParameters(parameters);
            getresult(url);
        }
    }

Open in new window

When the page load the scrollHandler gets called which is not what i wanted so i added a flag to detect the first scroll..
Why is that the scroll listener getting called on first loading of page... Is there any alternative to prevent it ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rohit Bajaj

ASKER

HI,
I tried this code ... Sometimes still on first page load the on scroll istener gets fired.. Dont know why...
I used the above code removing the lines :
if (firstScroll) {
            firstScroll = false;
            return;
        }

Because this is what i added specifically to stop the scrolling on firs loading...