Detect when the user has scrolled to the bottom of the page

var $win = $(window), $doc = $(document);

$win.on('scroll', function () {
    // 200px early, so the request is in flight before the user arrives
    if ($win.scrollTop() + $win.height() >= $doc.height() - 200) {
        loadNextPage();
    }
});