Perform an action when near the bottom of the page
<p>Javascript code:</p>

<pre><code>var next_page = 2;
var next_previews = 36;

function ajax_autopaginate(a, b) {
    function incrementPage () {

        next_page += 1;
        next_previews += 36;

    }

    jQuery('#' + b).load(a , function() {
        incrementPage();
        setTimeout(pollScrollPosition, 500);
    });
}

function pollScrollPosition() {
var y = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;

var max = window.scrollMaxY || (document.body.scrollHeight - document.body.clientHeight);

if ((max - y) &lt;= 1500) {
    ajax_autopaginate('http://www.wallpaperup.com/welcome/get_wallpapers/date_added/DESC/' + next_previews + '/','content' + next_page);
} else {
    setTimeout(pollScrollPosition, 500);
}
}

setTimeout(pollScrollPosition, 500);
</code></pre>

<p>I'm using this code to execute a function if we are 1500px or less from the end of the page, all works good in Firefox, but in IE and Chrome the function is always executed, how should I fix it?</p>

<p>this is the page that gives me problems: <a href="http://www.wallpaperup.com/" rel="nofollow">http://www.wallpaperup.com/</a></p>
