Rohit Bajaj
asked on
Scroll listener getting fired on initial page load
Hi,
I have the following js code :
Why is that the scroll listener getting called on first loading of page... Is there any alternative to prevent it ?
Thanks
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);
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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...