如何测量两次单击按钮之间的时间? [英] How do I measure the time between 2 clicks of a button?

查看:105
本文介绍了如何测量两次单击按钮之间的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个用户反复单击按钮以增加其得分的网站.为了防止人们作弊,我想测量每次点击之间的时间,如果他们的点击速度太快,并且两次点击之间的时间很少,我希望输入验证码或类似的内容.

我如何衡量点击之间的时间?

解决方案

我的建议如下:

$('button').click((function() {
    var history = [],
        last    = +new Date();

    return function(e) {
        history.push(e.timeStamp - last);

        console.log(history[history.length - 1]);
        last = e.timeStamp;
    };
}()));

这将输出&以毫秒为单位存储两次点击之间的差异.您可以使用历史记录数组获取平均值,然后检查平均值是否小于50ms.

演示: http://jsfiddle.net/TxKjT/

具有平均检查水平的演示: http://jsfiddle.net/TxKjT/2 /

I'm making a site where a user repeatedly clicks a button to increase his/her score. In order to prevent people cheating, I want to measure the amount of time between each click, and if they are clicking inhumanly fast and there is very little time between clicks, I want a CAPTCHA or something to come up.

How would I measure the time between clicks?

解决方案

My suggestion would look like:

$('button').click((function() {
    var history = [],
        last    = +new Date();

    return function(e) {
        history.push(e.timeStamp - last);

        console.log(history[history.length - 1]);
        last = e.timeStamp;
    };
}()));

This will output & store the difference between two clicks in miliseconds. You could use the history array to get an average value and check if that is below 50ms or something.

Demo: http://jsfiddle.net/TxKjT/

Demo with average check: http://jsfiddle.net/TxKjT/2/

这篇关于如何测量两次单击按钮之间的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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