I'm trying to implement infinite scroll for my Instagram-like app. However, the jQuery jode part that renders the next page of messages to be rendered is executed twice.
I tried adding a hack, but this is not entirely effective.
Here is the code:
var exec = false; $(window).scroll(function() { if ($('#paginator').length) { var url = $('#load_more').attr('href'); if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50 && !exec) { exec = true; $.get(url, function(data) { $('#paginator').append('<div class="spinner"></div>'); $.getScript(url); }).done(function(){ exec = false; }); } } });
The repeated part is obviously a duplicate of the ajax request, so a piece of messages is added twice.
How can I fix this problem?
source share