我正在尝试以设置的间隔触发随机的鼠标移动事件。能做到吗如果是,怎么办?
function random1() {
X = window.innerWidth / 2;
Y = window.innerHeight / 3;
$("canvas").trigger($.Event("mousemove", {clientX: X, clientY: Y}));
}
var active = false;
var interval = void 0;
function activate(event) { //'q' to activate
event.preventDefault();
if (event.keyCode === 81 && !active) {
active = true;
interval = setInterval(wallShift, 5000);
}
}
function deactivate(event) { //'e' to deactivate
event.preventDefault();
if (event.keyCode === 69) {
active = false;
clearInterval(interval);
}
}
window.addEventListener("keydown", activate, false);
window.addEventListener("keyup", deactivate, false);
答案 0 :(得分:-1)
我想我你有想要的东西
function random1() {
var min = 1;
var max = 4;
X = window.innerWidth / Math.floor(Math.random() * (max - min + 1)) + min;
Y = window.innerHeight / Math.floor(Math.random() * (max - min + 1)) + min;
$("canvas").trigger($.Event("mousemove", {
clientX: X,
clientY: Y
}));
}
var active = false;
var interval = void 0;
function activate(event) { //'q' to activate
event.preventDefault();
if (event.keyCode === 81 && !active) {
active = true;
interval = setInterval(random1, 1000);
}
}
function deactivate(event) { //'e' to deactivate
event.preventDefault();
if (event.keyCode === 69) {
active = false;
clearInterval(interval);
}
}
window.addEventListener("keydown", activate, false);
window.addEventListener("keyup", deactivate, false);