Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q602 Is it possible to create a script that will refresh a page only when the mouse has been inactive?

irt.org | Knowledge Base | JavaScript | Pointer | Q602 [ previous next ]

Q602 Is it possible to create a script that will refresh a page only when the mouse has been inactive?

Try this:

<html>
<head>

<script language="JavaScript"><!--
if (navigator.appName == 'Netscape') {
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = netscapeMouseMove;
}

function doit() {
    timerRunning = false;
    alert('hello world');
}

function netscapeMouseMove(e) {
    if (e.screenX != document.test.x.value && e.screenY != document.test.y.value) {
        if (timerRunning) {
            clearTimeout(myTimer);
            myTimer = setTimeout('doit()',5000);
        }
        document.test.x.value = e.screenX;
        document.test.y.value = e.screenY;
    } 
}

function microsoftMouseMove() {
    if (window.event.x != document.test.x.value && window.event.y != document.test.y.value) {
        if (timerRunning) {
            clearTimeout(myTimer);
            myTimer = setTimeout('doit()',5000);
        }
        document.test.x.value = window.event.x;
        document.test.y.value = window.event.y;
    }
}

var myTimer = setTimeout('doit()',5000); // invoke doit() after five seconds of mouse inactivity
var timerRunning = true;
//--></script>

</head>

<body onMousemove="microsoftMouseMove()">

<form name="test"><input type="hidden" name="x"><input type="hidden" name="y"></form>

</body>
</html>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.