gpt4 book ai didi

javascript 快捷键/停止 间隔功能

转载 作者:行者123 更新时间:2023-11-27 23:15:16 29 4
gpt4 key购买 nike

我为javascript函数制作了一个快捷键。我设置了 S 键来启动此功能,因此设置了 Z 键来清除间隔功能,但我对此感到厌倦,当按 Z 键时,间隔不会停止:(

var isCtrl = false;
document.onkeydown=function(e){
if(e.which == 83) {




var elem = document.elementFromPoint( cursorX,cursorY );
elem.addEventListener('click', function() {
console.log('clicked')
}, false);

var support = true;

try {
if (new MouseEvent('click', {bubbles: false}).bubbles !== false) {
support = false;
} else if (new MouseEvent('click', {bubbles: true}).bubbles !== true) {
support = false;
}
} catch (e) {
support = false;
}

var refreshIntervalId = setInterval(function() {
if (support) {
var event = new MouseEvent('click');
}else{
var event = document.createEvent('Event');
event.initEvent('click', true, true);
}
elem.dispatchEvent(event);
},10);
var cursorX;
var cursorY;
cursorX = 0; cursorY = 0;
document.onmousemove = function(e){
cursorX = e.clientX;
cursorY = e.clientY;
elem = document.elementFromPoint(e.clientX, e.clientY);
}


if(e.which == 90) {
clearInterval(refreshIntervalId);
}

}
}

帮帮我,我想按 Z 键和间隔停止,但我不能......

更新:此代码可以正常工作。使用S键启动功能,使用Z键停止功能。

var elem = document.elementFromPoint( cursorX,cursorY );
elem.addEventListener('click', function() {
console.log('clicked')
}, false);

var support = true;

try {
if (new MouseEvent('click', {bubbles: false}).bubbles !== false) {
support = false;
} else if (new MouseEvent('click', {bubbles: true}).bubbles !== true) {
support = false;
}
} catch (e) {
support = false;
}


var cursorX;
var cursorY;
cursorX = 0; cursorY = 0;
document.onmousemove = function(e){
cursorX = e.clientX;
cursorY = e.clientY;
elem = document.elementFromPoint(e.clientX, e.clientY);
}



var refreshIntervalId;
window.addEventListener("onkeydown", keyDown,true);
window.addEventListener("keydown", keyDown);

function keyDown() {
var e = window.event;
switch (e.keyCode) {
case 83:
start();
break;
case 90:
stop();
break;
}
}

function start() {
stop();
refreshIntervalId = setInterval(function() {
if (support) {
var event = new MouseEvent('click');
}else{
var event = document.createEvent('Event');
event.initEvent('click', true, true);
}
elem.dispatchEvent(event);
},1000);
}

function stop() {
if (refreshIntervalId != null) {
clearInterval(refreshIntervalId);
refreshIntervalId = null;
}
}

最佳答案

您还必须以某种方式对其进行编码,以避免多次启动计时器。您应该像这样构造它(将计时器的 var 移到函数之外):

var refreshIntervalId;
window.addEventListener("onkeydown", keyDown,true);
window.addEventListener("keydown", keyDown);

function keyDown() {
var e = window.event;
switch (e.keyCode) {
case 83:
start();
break;
case 90:
stop();
break;
}
}

function start() {
stop();
refreshIntervalId = setInterval(function() {
// code...
},10);
}

function stop() {
if (refreshIntervalId != null) {
clearInterval(refreshIntervalId);
refreshIntervalId = null;
}
}

关于javascript 快捷键/停止 间隔功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35876855/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com