I'm building an infinite Scroll which fetches results from a database when the scroller hits the bottom of the page. It's all working fine except for one small thing; it is fetching the results twice if I scroll down fast, as if the function is executed twice.
Here is my jQuery code:
$(window).scroll(function () {
if($(window).scrollTop() == $(document).height() - $(window).height()) {
var ID = $('.stbody:last').attr('id').match(/stbody(\d+)/)[1];
$('#loader').show();
$.ajax({
url: "ajax_loadmore.php?lCom=" + ID,
success: function (result) {
if(result) {
$('#moreComments').append(result);
$('#loader').hide();
} else {
$('#loader').hide();
}
}
});
}
});