Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts

    document.onmousemove question?

    Hi I am trying to make a function run if the mouse is moved over the document but when using the object onmousemove it seems to run the code even if the mouse is still over then document, how can I make it so if the mouse is over the document but isn't moving then don't run the code but once the mouse moves run the code?

    This is the code I made to handle the mouse move collections.
    Code:
    function onmove(func) {
      var oldonmove = document.onmousemove;
      if(typeof document.onmousemove != 'function') {
        document.onmousemove = func;
      } else {
        document.onmousemove = function() {
          oldonmove();
          func();
        };
      }
    }
    And to use it you would do this.
    Code:
    <script type="text/javascript">
    /* Alert A Message Everytime The User Moves Their Mouse */
    onmove(function(){ alert('You Moved!'); });
    </script>
    But with this code it runs even when the user doesn't move their mouse and the notification box pops up every second as the code seems to think a still mouse is a moving mouse.

    I was thinking about having a run once system but that would mean if the mouse moves it runs once and then if the mouse moves again the code will not run as it has already ran before.

    If anyone can help me please reply, Thank You.

    DJCMBear.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #2
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 3 Times in 3 Posts
    The issue that you are seeing is your use of alert in your test code.
    Code:
    onmove(function(){ alert('You Moved!'); });
    Whenever alert is called by your javascript the browser loses focus and so when the focus is returned to the browser after the alert is cleared, the browser generates another mousemove event.

    If used different code in your test case fore example appending a text node to your document:
    Code:
    var count = 0;
    onmove(function(){
       var br = document.createElement('br');
       var txt = document.createTextNode("You have moved the mouse " + count++ + " times.");
                
       document.body.appendChild(txt);
       document.body.appendChild(br);
    
       window.scrollTo(0,document.body.offsetHeight);
    });
    Or for ease of viewing just replaced the text inside of the body: (Though using innerHTML being bad practice from my understand)
    Code:
    var count = 0;
    onmove(function(){
       document.body.innerHTML = "You have moved the mouse " + count++ + " times.";
    });
    You probably would have never thought your code was working improperly.

    Regards,
    JLBShecky (A novice web programmer obsessed with HTML5)

    EDIT: Tested in IE8, IE8 compatibility mode, Chrome 8.0.552.215, FireFox 3.5.15, Safari 5.0.3, and Opera 10.63. I only tested on windows platforms as that is all I have in front of me at the moment, but that should be irrelevant in this case.
    Last edited by JLBShecky; 12-06-2010 at 09:38 PM. Reason: Correct 2nd code snipet and added browser tested in

  • Users who have thanked JLBShecky for this post:

    DJCMBear (12-06-2010)

  • #3
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Thanks for the reply and I now know what im doing with it but how can I do a thing similar to facebook where if you are on a different tab for a while or inactive for like 1min and then move you mouse anywhere on the document window the live update render newer updates how can I do something like that where it doesn't call the updates when ever the mouse is moved but only if the window was inactive?
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #4
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I didn't mean to come across as condescending. I consider myself to be a novice web programmer, and for all I know you know more about web programming than I do. I was just attempting to be through, one never knows when some time down the road someone might find a topic to be useful.

    That said in the example of retrieving new content if user has been inactive for a certain amount of time I would personally do it so that I keep track of the last time that the user moved the mouse and if the time was greater than a specified delta I would retrieve new content. I would probably use the date object and its getTime function to get the unix time stamp in milliseconds and compare it to a stored value from the last time the user moved the mouse.

  • Users who have thanked JLBShecky for this post:

    DJCMBear (12-06-2010)

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    lol well I don't mean to sound rude or anything but if you go look at my profile that lists not even half the programming languages I know lol and yes it is nice to give ideas on others topics lol.

    Thanks for the idea that's given me the best idea of what I shell do to make it work, then again I may just do a twitter type live update lol.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #6
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Just glad to be of some help, hopefully your new best idea will work out.

    Personally the only other thing that I would probably do is to integrate the use of setInterval into the code. Probably have some code called by setInterval update the content if the mouse has been move in the last minute.

    -JLBShecky
    PS - And no you weren't coming across as being rude, I jsut thought I may have come across as such.

  • Users who have thanked JLBShecky for this post:

    DJCMBear (12-06-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •