简体   繁体   English

如何衡量javascript中点击和发布之间的时间?

[英]How can I measure the time between click and release in javascript?

I want to start a timer when the user clicks on a object, and stop it when the user releases the click. 我想在用户点击一个对象时启动一个计时器,并在用户释放点击时停止它。 All using javascript/jquery. 全部使用javascript / jquery。

The following should get you started 以下内容应该让您入门

var start_time;
function start() {
    start_time = new Date();
}
function end() {
    var now = new Date();
    alert(now-start_time);
}

$('#element_id').mousedown(start);
$('#element_id').mouseup(end);

the mousedown event will run the start function which sets the start time. mousedown事件将运行start函数,该函数设置开始时间。 the mouseup event will substract the start time from the current time. mouseup事件将从当前时间减去开始时间。 The result is in milliseconds 结果以毫秒为单位

$("#element").mousedown(); will let you record when the user clicks, and similarly $("#element").mouseup(); 将让你记录用户点击的时间,同样$("#element").mouseup(); will let you record when the button is released. 将让你记录按钮被释放的时间。

var start = 0;
$("#element").mousedown(function() { start = new Date(); });
$("#element").mouseup(function() {
    var cur = new Date();
    alert(cur-start);
}
function myTimer() {
    doSomething();
}

$('#thing').mousedown(function() {
    $(this).data('timerHandle', setTimeout(myTimer));
});
$('#thing').mouseup(function() {
    clearTimeout($(this).data('timerHandle'));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM