I want an element to appear in case the website has not been used for a while and make it disappear after it is being used again. Is there any way to detect on a website if the mouse has not been moved for awhile?

closed as off-topic by Robert Harvey, gnat, Kilian Foth, Dan Pichelman, Bart van Ingen Schenau Sep 2 '13 at 17:55

  • This question does not appear to be about software engineering within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    This question appears to be off-topic because it is about an implementation issue. – Robert Harvey Sep 2 '13 at 3:24
  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Sep 2 '13 at 7:23
  • Ok sorry guys, I will ask better next time. – Karolis Ramanauskas Sep 2 '13 at 7:43

Put an event handler that takes mouse position whenever mouse is moved store it in variable and store the exact time that happened.

Make a timer that runs each lets say each minute and checks if the last stored mouse position and time is older than a minute then make that element you want to appear else make sure the hide that element.

  • 2
    This is simple enough, and it'll work. You don't really have to store the exact time, you can just increment a flag every x seconds: Detecting idle time in JavaScript elegantly – yannis Sep 1 '13 at 11:50
  • It worked great. I used @YannisRizos link if anyone else needs this. – Karolis Ramanauskas Sep 1 '13 at 11:59
  • 2
    What about touch devices? What about people using the keyboard to navigate the page? Sure, you've detected the mouse movements but what about the bigger issue? – Sean McSomething Sep 1 '13 at 23:44
  • Good point @SeanMcSomething . My website has a really specific use case so I only need the mouse but would be interesting to hear solutions for keyboard, etc. – Karolis Ramanauskas Sep 2 '13 at 18:52

You can set up a timer that expires after the desired amount of time of mouse inactivity, and reinitialize it whenever the mouse is moved.

See this question for an example (clearTimeout followed by setTimeout).

Not the answer you're looking for? Browse other questions tagged or ask your own question.