gpt4 book ai didi

javascript - html Canvas 中的关键灵敏度

转载 作者:行者123 更新时间:2023-11-30 15:06:35 29 4
gpt4 key购买 nike

我需要知道在松开鼠标/按键之前按下了多长时间,因此响应将取决于按键/鼠标的按下。

例如,我试过这个,但出于某种原因它似乎不起作用。

var time = new Date();
var mousedown, mouseup, mousehold;

document.addEventListener('onmousedown', function(){
mousedown = time.getTime(); //gets time when mouse was pressed
});

document.addEventListener('onmouseup', function(){
mouseup = time.getTime(); //time when mouse was released
mousehold = mouseup - mousedown; //time difference
console.log(mousehold);
});

最佳答案

首先你应该使用

document.addEventListenter("mousedown", function() {...});

document.addEventListener("mouseup", function() {...}); '

其次,您的time.getTime 将获取time 变量的时间。你应该这样做:

var mousedown, mouseup, mousehold;

document.addEventListener('mousedown', function(){
mousedown = Date.now(); //gets time when mouse was pressed
});

document.addEventListener('mouseup', function(){
mouseup = Date.now(); //time when mouse was released
mousehold = mouseup - mousedown; //time difference
console.log(mousehold);
});

这是一个更好的版本:

var mouseup, mousehold;

document.addEventListener('mousedown', function(){
mousedown = Date.now(); //gets time when mouse was pressed
});

document.addEventListener('mouseup', function(){
mousehold = Date.now()- mousedown; //time difference
console.log(mousehold);
});

如果您只想计算 mousehold 变量,则无需使用 mouseup 变量。

关于javascript - html Canvas 中的关键灵敏度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45654548/

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