I currently have the following function which effectively retrieves JSON from Django and loads movies via infinite scroll. However, I find that sometimes this event triggers multiple times, and it ends up getting the same page twice.
$(window).scroll(function() {
var break_point = $(document).height() - ($(window).height() * 1.02);
if ($(window).scrollTop() >= break_point) {
var timePeriod = $('.tab-content').find('.active').attr('id');
var nextPage = $('#'+timePeriod+' ul li:last').attr('data-next');
if (nextPage) {
loadMovies(timePeriod, nextPage);
}
}
});
What's the best way to stop this listener from executing multiple times per page?