Home > Back-end >  How to track mouse movement in JavaScript
How to track mouse movement in JavaScript

Time:10-24

I have a little problem, i can't figure out how to track mouse position and update a function every time it moves, this is the code that i want to modify so it updates;

function makeid() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 8; i  )
    text  = possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

console.log(makeid());
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Hopefully one of the smart people out there could help me!

CodePudding user response:

@BadPiggie I mean that so every time my code tracks mouse movement the password regenerates.

As you mentioned in the comment, If you want to trigger the makeid evener the mouse move, You can listen to mousemove event on window

function makeid() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 8; i  )
    text  = possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}

function mouseMoveEventHandler() {
  console.log(makeid());
}

window.addEventListener('mousemove', mouseMoveEventHandler);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related