英文:
How to set trigger on 3 times Shift press in JS?
问题
function Press(){
document.querySelector("body").addEventListener("keyup", event => {
if(event.key !== "Shift") return;
document.getElementById("myElement").click();
event.preventDefault();
});
}
UPDATE:
这是经 @deceze 帮助制作的工作代码
function Press(){
document.addEventListener("keyup", event => {
if(event.key !== "Shift") return;
shift_pressed = typeof shift_pressed !== 'undefined' ? shift_pressed : 0;
shift_pressed++;
setTimeout(() => {
shift_pressed= shift_pressed-1 >= 0 ? shift_pressed-1 : 0;
}, 1000);
if (shift_pressed===3){
document.getElementById("myElement").click();
shift_pressed=0;
}
event.preventDefault();
});
}
感谢大家的帮助。
英文:
function Press(){
document.querySelector("body").addEventListener("keyup", event => {
if(event.key !== "Shift") return;
document.getElementById("myElement").click();
event.preventDefault();
});
}
I set keyup for press Shift 1 time. How can I set it for 3 fast times? So that after Shift Shift Shift an event (document.getElementById("myElement").click()) is executed?
UPDATE:
this is working code made with @deceze help
function Press(){
document.addEventListener("keyup", event => {
if(event.key !== "Shift") return;
shift_pressed = typeof shift_pressed !== 'undefined' ? shift_pressed : 0;
shift_pressed++;
setTimeout(() => {
shift_pressed= shift_pressed-1 >= 0 ? shift_pressed-1 : 0;
}, 1000);
if (shift_pressed===3){
document.getElementById("myElement").click();
shift_pressed=0;
}
event.preventDefault();
});
}
thanks everyone for the help
答案1
得分: 1
function Press(){
const THRESHOLD = 250;
let counter = 0;
let millis = Date now();
document.querySelector("body").addEventListener("keyup", event => {
if(event.key !== "Shift") return;
if(Date now()-millis > THRESHOLD) counter = 0;
if(++counter%3===0) {
document.getElementById("myElement").click();
event.preventDefault();
}
millis = Date now();
});
}
英文:
That should trigger the click on every third press of the Shift key
function Press(){
const THRESHOLD = 250;
let counter = 0;
let millis = Date.now();
document.querySelector("body").addEventListener("keyup", event => {
if(event.key !== "Shift") return;
if(Date.now()-millis > THRESHOLD) counter = 0;
if(++counter%3===0) {
document.getElementById("myElement").click();
event.preventDefault();
}
millis = Date.now();
});
}
答案2
得分: 0
只需为清除点击<kbd>Shift</kbd>计数的setTimeout
创建代码,并定义clearTimeout
来清除每次单击的超时。
document.getElementById("myElement").onclick = () => {
console.log('Hello World!');
}
let clearTime = 1000;
let counterShift = 0;
let timeout;
document.querySelector("body").addEventListener("keyup", event => {
if(event.key !== "Shift") return;
clearTimeout(timeout);
counterShift++;
console.log('number click:', counterShift);
if(counterShift == 3) {
document.getElementById("myElement").click();
event.preventDefault();
counterShift = 0;
}
timeout = setTimeout(() => {
counterShift = 0;
}, clearTime);
});
<button id="myElement">Test</button>
英文:
Just create setTimeout
for clearing counter of clicking <kbd>Shift</kbd> and define clearTimeout
clear the timeout for every single click.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.getElementById("myElement").onclick = () => {
console.log('Hello World!');
}
let clearTime = 1000;
let counterShift = 0;
let timeout;
document.querySelector("body").addEventListener("keyup", event => {
if(event.key !== "Shift") return;
clearTimeout(timeout);
counterShift++;
console.log('number click:',counterShift);
if(counterShift == 3) {
document.getElementById("myElement").click();
event.preventDefault();
counterShift = 0;
}
timeout = setTimeout(()=>{
counterShift = 0;
}, clearTime);
});
<!-- language: lang-html -->
<button id="myElement">Test</button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论