能否查询/检测网页用户的双击速度?

发布于 2024-12-18 21:19:18 字数 116 浏览 3 评论 0原文

它取决于操作系统/用户。不是浏览器,不是网站,而是操作系统决定双击的速度。

我想在我的应用程序中使用该号码。有没有办法用JS获取这个数字?

简单的问题。也许不可能。

谢谢

It's OS/user dependant. Not the browser, not the website, but the OS decides how fast and slow a double click must be.

I'd like to use that number in my app. Is there a way to get that number with JS?

Simple question. Might not be possible.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太傻旳人生 2024-12-25 21:19:18

简单的答案:不,抱歉。

您能做的最好的事情就是这样(示例使用 jQuery 只是因为它编写起来更快,如果 jQuery 不可用,则原则成立。另请注意,这也可能是被简化,这正是首先想到的):

var timer,
    num = 0;
$("#example").click(function() {
    /*This condition is required because 2 click events are fired for each
    dblclick but we only want to record the time of the first click*/
    if(num % 2 === 0) {
        timer = (new Date()).getTime();
    }
    num++;
}).dblclick(function() {
    var time2 = (new Date()).getTime(),
        dblClickTime = time2 - timer; 
});

不幸的是,这可能不是很有帮助。您也许可以记录 dblClickTime 值并检查最长的值,但这仍然不太可能是您想要的实际值。 JavaScript 无法实现此类功能。

Simple answer: no, sorry.

The best you could do would be something like this (example uses jQuery simply because it was quicker to write, the principle holds if jQuery is unavailable. Also note that this could well be simplified, this is just what came to mind first):

var timer,
    num = 0;
$("#example").click(function() {
    /*This condition is required because 2 click events are fired for each
    dblclick but we only want to record the time of the first click*/
    if(num % 2 === 0) {
        timer = (new Date()).getTime();
    }
    num++;
}).dblclick(function() {
    var time2 = (new Date()).getTime(),
        dblClickTime = time2 - timer; 
});

Unfortunately, that's probably not very helpful. You may be able to record the dblClickTime values and check for the longest, but that still is very unlikely to be the actual value you're after. That sort of thing is just not available through JavaScript.

眼眸里的快感 2024-12-25 21:19:18

据我所知,2021 年的答案仍然没有。这是有原因的:我们不应该关心。

原则上,dblclick 在某种程度上已经过时了……

我们有一个不太为人所知的detail 属性。也许是因为名字。

来自 MDN

传递到单击事件处理程序的 MouseEvent 对象将其详细属性设置为目标被单击的次数。换句话说,双击的详细信息将为 2,三次单击的详细信息将为 3,依此类推。在没有任何点击发生的情况下,该计数器会在短时间间隔后重置;该间隔的具体时间可能因浏览器和平台的不同而有所不同。该间隔也可能受到用户偏好的影响;例如,辅助功能选项可能会延长此间隔,以便更轻松地使用自适应界面执行多次点击。

带有 detail 即。 click_count 当detail != 1时,可以停止CLICK的传播

所以伪代码:

if evt.detail==1
    do_click()
if evt.detail==2
    do_dblclick()
...
    
if evt.detail!=1
    evt.stopPropagation()   
    

如果有人真的需要区分单击、双击、三击……就像“XOR”一样,他们应该真正重新考虑设计。

DblClickTime 可能会很长,这意味着如果用户只想单击操作,则应用程序感觉没有响应。

另一个问题是,用户的意图可能是双击,但速度很慢 - 然后有两个单击操作,它们不应该与 dblclick 不同。

Answer 2021 - as far as I know - still not. There is a reason: we should not care.

In principle dblclick is somehow obsolete …

We have the not well known detail property. Maybe because of the name.

From MDN:

The MouseEvent object passed into the event handler for click has its detail property set to the number of times the target was clicked. In other words, detail will be 2 for a double-click, 3 for triple-click, and so forth. This counter resets after a short interval without any clicks occurring; the specifics of how long that interval is may vary from browser to browser and across platforms. The interval is also likely to be affected by user preferences; for example, accessibility options may extend this interval to make it easier to perform multiple clicks with adaptive interfaces.

With detail ie. click_count it is possible to stop propagation of CLICK when detail != 1

So pseudcode:

if evt.detail==1
    do_click()
if evt.detail==2
    do_dblclick()
...
    
if evt.detail!=1
    evt.stopPropagation()   
    

If someone really needs to distinguish between click, double-click, triple-click, … like an 'XOR', they should really rethink the design.

The DblClickTime can be very long, that means the app feels like not responding, if the user just wants the click-action.

The other problem is, that it is possible, that users intention is a double-click, but is to slow - then there are two click-actions, they should not be to different to dblclick.

冷情妓 2024-12-25 21:19:18

我想在我的应用程序中使用该号码。有没有办法用JS获取这个数字?

绝对不是——这样的东西超出了 JavaScript 的范围。

您可以通过要求用户双击来找出适用于双击的值,侦听 click 事件并查看 dblclick 事件 被触发 - 不过,我不确定事件处理是否是这样工作的。但即使这有效,距离真正找出实际价值还有很长的路要走。

I'd like to use that number in my app. Is there a way to get that number with JS?

Definitely not - stuff like this is outside JavaScript's scope.

You may be able to find out values that work for a double click by asking the user to double-click, listen to the click events and see whether the dblclick event is fired - I'm nnot sure whether event handling works that way, though. But even if that works, it is still a long way from actually finding out the actual value.

知足的幸福 2024-12-25 21:19:18

这是我 2015 年的解决方案,希望看到一个纯 js 版本。

var start;
var click = null;

$(document).click(function() {
    var now = performance.now();
    start = click ? click : now;
    click = now;

}).dblclick(function() {
    alert(performance.now()-start)
});

编辑

纯JS

var start;
var click = null;

var getStart = function() {
    var now = performance.now();
    start = click ? click : now;
    click = now;
}
var getStop = function() {
    alert(performance.now()-start)
}


if (window.addEventListener) {
    window.addEventListener('click', getStart , false);
} else {
    window.attachEvent('onclick', function() {
        return(getStart.call(window, window.event));   
    });
}
if (window.addEventListener) {
    window.addEventListener('dblclick', getStop , false);
} else {
    window.attachEvent('ondblclick', function() {
        return(getStop.call(window, window.event));   
    });
}

This is my 2015 solution, would like to see a pure js version tho.

var start;
var click = null;

$(document).click(function() {
    var now = performance.now();
    start = click ? click : now;
    click = now;

}).dblclick(function() {
    alert(performance.now()-start)
});

EDIT

Pure JS

var start;
var click = null;

var getStart = function() {
    var now = performance.now();
    start = click ? click : now;
    click = now;
}
var getStop = function() {
    alert(performance.now()-start)
}


if (window.addEventListener) {
    window.addEventListener('click', getStart , false);
} else {
    window.attachEvent('onclick', function() {
        return(getStart.call(window, window.event));   
    });
}
if (window.addEventListener) {
    window.addEventListener('dblclick', getStop , false);
} else {
    window.attachEvent('ondblclick', function() {
        return(getStop.call(window, window.event));   
    });
}
甲如呢乙后呢 2024-12-25 21:19:18

补充 James Allardice 的答案:

根据您的实现以及您正在寻找双击的位置,您可能还需要检查用户的鼠标位置(或者我猜是点击位置)。这是为了避免当用户单击页面不同部分上的内容时双击触发(再次取决于您的事件侦听器实现 - 例如,如果它只是在一个按钮上,这可能不是问题)。

当点击事件触发时,下面示例中的事件侦听器有两个变量 e.clientXe.clientY。这将为您提供鼠标的位置。您可能想要检查用户自第一次单击以来是否显着移动了鼠标(相应地适应您的代码)。

document.addEventListener("click", function(e){ console.log("Mouse X: " + e.clientX + ": Mouse Y: " + e.clientY); });

您不希望它太紧,否则用户可能永远无法双击,并且您不希望它太松,以免用户看似随机地双击。也许从第一次点击周围 25px 左右的框开始(同样这取决于您的应用程序)。您可以根据您的用户界面进行测试和调整。

我假设您没有 jQuery 或没有使用它,因为我相信 jQuery 可能已经执行此计算来触发 dblclick

Adding on to James Allardice's answer:

Depending on your implementation and where you are looking for double clicks you may want to also check the users mouse location (or I guess tap location). This is to avoid a double click firing when the user is clicking things on different parts of your page (again depends on your event listener implementation -- if it is just on one button for example this probably isn't an issue).

When a click event fires the event listener in my example below has two variables e.clientX and e.clientY. This will give you the location of the mouse. You might want to check to see if the user has moved their mouse significantly since the first click (adapt accordingly to your code).

document.addEventListener("click", function(e){ console.log("Mouse X: " + e.clientX + ": Mouse Y: " + e.clientY); });

You don't want to have it be too tight or else a user may never be able to fire a double click, and you don't want it to be too loose so that double clicks fire seemingly randomly for the user. Maybe start with a 25px or so box around the first click (again this depends on your application). This is something you can test and adjust based on your user interface.

I am assuming you don't have jQuery or aren't using it, because I believe jQuery might already do this calculation to fire dblclick

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文