How to: Refresh your WordPress page unless a comment is being typed

A customer wanted a piece of code that allowed a page to be refreshed once the client has remained on the page after a certain amount of time. They’d used a refresh command previously, but the problem was that this interrupted anyone who was in the middle of typing a comment.

This is my solution to refresh the page every 5 minutes:

<script language="JavaScript">
    var sURL = unescape(window.location.pathname);
    var intValue = 0;
    function doLoad()
    {
        intValue=setTimeout( "refresh()", 300*1000 );
    }

    function refresh()
    {
        window.location.href = sURL;
    }

    function noRefresh(e)
    {
        switch (e.keyCode) {
            case 40:
            case 39:
            case 38:
            case 37:
            case 34:
            case 33:
                break;
            default:
                clearTimeout(intValue);
        }

    }

    if ($.browser.mozilla) {
        $(document).keypress(noRefresh);
    } else {
        $(document).keydown(noRefresh);
    }

    $(document).ready(doLoad());
</script>
<noscript>
    <meta http-equiv="refresh" content="300">
</noscript>

  • jaffa

    Great blog.

  • Nice thanks very much

  • rtrt

  • I don’t think meals have any business being deductible. I’m for separation of calories and corporations.
    The meek shall inherit the planet earth, and not its mineral rights.