?
Solved

Javascript: OnMouseMove event problem

Posted on 2009-05-07
4
Medium Priority
?
522 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
janjerell earned 2000 total points
ID: 24333581
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
ID: 24333684
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
ID: 24333777
sorry, it works fine, is my mistake. Thanks a lot.
0
 
LVL 3

Expert Comment

by:janjerell
ID: 24334135
Thanks... Glad to help...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 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