var scrollListener = function () { $(window).one("scroll", function () { if($(window).scrollTop() + 256 >= $(document).height() - $(window).height()) { $.ajax({ url: './api/p/'+$('#posts').data('last'), dataType: 'json', success: function(msg) { var html = ""; for(var i = 0; i < msg.items.length; i++) if(msg.items[i].id) html += "\n"; $('#posts').append(html); $('#posts').data('last', msg.last); } }); } setTimeout(scrollListener, 50); }); }; $(document).ready(function () { scrollListener(); });