Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capture mouse position on setInterval() in Javascript

I have a function in Javascript that moves one div depending on the mouse position. This function is set on a setInterval() function and executed every second. I need to capture the mouse position like this:

function mousemov() {
  document.getElementById("myDiv").style.left = Event.clientX; //don't work
}

window.onload = function() {
  setInterval("mousemov()",1000);
}

Ps: I cannot use the mousemove event because the function must be executed even if the mouse is stopped.

like image 691
rizidoro Avatar asked Oct 22 '09 16:10

rizidoro


People also ask

How do you get the mouse pointer position?

Once you're in Mouse settings, select Additional mouse options from the links on the right side of the page. In Mouse Properties, on the Pointer Options tab, at the bottom, select Show location of pointer when I press the CTRL key, and then select OK. To see it in action, press CTRL.

What does setInterval () method do in JS?

The setInterval() function is commonly used to set a delay for functions that are executed again and again, such as animations. You can cancel the interval using clearInterval() . If you wish to have your function called once after the specified delay, use setTimeout() .

What is the correct syntax to call the setInterval () function?

Syntax: const intervalId = setInterval(func, [delay, arg1, agr2, ..., argN]); where, func is the function that we want to execute repeatedly after delay milliseconds.

Is setInterval a callback function?

The setInterval() is a method of the window object. The setInterval() repeatedly calls a function with a fixed delay between each call. In this syntax: The callback is a callback function to be executed every delay milliseconds.


2 Answers

The only time that you can access the event object is during the execution of an event handler. So what you need to do is create an OnMouseMove event on the document and store the mouse coordinates in a globally accessible object. You can then access these values from anywhere else in your script to determine the mouse position.

Here is an example (you're not using jQuery, so this is straight DOM code):

document.onmousemove = function(e) {
    var event = e || window.event;
    window.mouseX = event.clientX;
    window.mouseY = event.clientY;
}

function mousemov() {
    document.getElementById("myDiv").style.left = window.mouseX;
}

window.onload = function() {
    setInterval(mousemov, 1000);
}

I should make the note that clientX and clientY don't take scrolling into account. You'll need to retrieve the scrolling offsets and apply them to the returned values.

like image 63
Jon Benedicto Avatar answered Sep 21 '22 02:09

Jon Benedicto


well, if you listen to mouse move for the document and save its location, then, whenever you want, e.g. every second in your case you have the latest registered mouse position.

this is a jquery example

$(document).ready(function()
 {
  $().mousemove(function(e)
   {
       window.mouseX = e.pageX;
       window.mouseY = e.pageY;
  });
});

and your mousemove function would be

function mousemov() { 
    document.getElementById("myDiv").style.left = window.mouseX;
}
like image 22
Tzury Bar Yochay Avatar answered Sep 20 '22 02:09

Tzury Bar Yochay