如何在 JavaScript 中跟踪鼠标移动

How to track mouse movement in JavaScript

我有一个小问题,我不知道如何跟踪鼠标位置并在每次移动时更新一个函数,这是我要修改的代码,因此它会更新;

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());

希望其中一位聪明人能帮助我!

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

正如您在评论中提到的,如果您想在鼠标移动时触发 makeid,您可以在 window

上收听 mousemove 事件

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);