Solved

Javascript: OnMouseMove event problem

Posted on 2009-05-07
4
511 Views
Last Modified: 2012-05-06
I have a count down javascript to count down in a webpage. This count down timer will be reset once user move the mouse, however, when a msn,yahoo messager or any chat application alert appear, it will reset the count down timer. I just want the timer will reset if the user move the mouse. Below is the html code. Can i have your help.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
 

<head>

    <title>Testing</title>

	    <script type="text/javascript">

            var timeActivity;

            var seconds;

            document.d.d2.value='0';

            function start()

            {

                seconds=300;

                countDown();

                

	            document.documentElement.onmousemove = function() 

	            {

	                seconds = 300;

	                countDown();

	            }

            }
 

            function countDown()

            {

                 document.d.d2.value = seconds;

                 seconds-=1;

                 clearTimeout(timeActivity);

                 timeActivity = setTimeout("countDown()",1000) 

            }

            

            

	    </script>		

</head>
 

<body>

    <form name="d">

        <p><input type="text" size="8" name="d2"/></p>

        <input type="button" value="Start" name="Start" onclick="start()" />

    </form>

</body>
 

</html>

Open in new window

0
Comment
Question by:sawyih1
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
janjerell earned 500 total points
Comment Utility
Hello sawyih1,


you can try this code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head>

    <title>Testing</title>

            <script type="text/javascript">

            var timeActivity;

            var seconds;

	    var isWindowActive = true;

window.onblur = windowBlurred;

window.onfocus = windowFocused;
 

function windowBlurred() { isWindowActive = false; }

function windowFocused() { isWindowActive = true; }
 

            document.d.d2.value='0';

            function start()

            {

                seconds=300;

                countDown();

                

                    document.documentElement.onmousemove = function() 

                    {

			if(isWindowActive == true)

			{

				//alert(isWindowActive);

                        	seconds = 300;

                        	countDown();

			}

                    }

            }

 

            function countDown()

            {

                 document.d.d2.value = seconds;

                 seconds-=1;

                 clearTimeout(timeActivity);

                 timeActivity = setTimeout("countDown()",1000) 

            }

            

            

            </script>           

</head>

 

<body>

    <form name="d">

        <p><input type="text" size="8" name="d2"/></p>

        <input type="button" value="Start" name="Start" onclick="javascript:start();" />

    </form>

</body>

 

</html>

            

            //
 

            //document.window.onblur = function () { alert('a');  isWindowActive = false; }
 

            //document.window.onfocus = function () { alert('a'); isWindowActive = true; }

             

Open in new window

0
 

Author Comment

by:sawyih1
Comment Utility
Hi Janjerell,thanks for the help. But it works in html page, but it doesn't work correctly when i put inside aspx page. The timer still running after i switch back from other window.Do u have any idea?
0
 

Author Comment

by:sawyih1
Comment Utility
sorry, it works fine, is my mistake. Thanks a lot.
0
 
LVL 3

Expert Comment

by:janjerell
Comment Utility
Thanks... Glad to help...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
A procedure for exporting installed hotfix details of remote computers using powershell
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now