如何衡量mousedown和mouseup之间的毫秒数? [英] How to measure the milliseconds between mousedown and mouseup?

查看:181
本文介绍了如何衡量mousedown和mouseup之间的毫秒数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法测量鼠标发布之间的毫秒数?

Is there any way to measure the number of milliseconds between mouse press and release?

推荐答案

您可以创建一个闭包来共享两个变量,一个用于存储开始时间,另一个用于结束时间,然后在mouseup事件中,获得差异:

You could create a closure to share two variables, one to store the start time and other for the end time, then in the mouseup event, get the difference:

(function () {
    var element = document.getElementById('element'),
        start, end;

    element.onmousedown = function () {
      start = +new Date(); // get unix-timestamp in milliseconds
    };


    element.onmouseup = function () {
      end = +new Date();

      var diff = end - start; // time difference in milliseconds
    };

})();

检查此工作示例

这篇关于如何衡量mousedown和mouseup之间的毫秒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆