php - How do I measure the time between 2 clicks of a button? - Stack Overflow

I'm making a site where a user repeatedly clicks a button to increase hisher score. In order to p

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 e up.

How would I measure the time between clicks?

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 e up.

How would I measure the time between clicks?

Share Improve this question edited Jul 5, 2013 at 14:19 Smi 14.4k9 gold badges61 silver badges65 bronze badges asked Mar 5, 2011 at 13:22 TaimurTaimur 3,2518 gold badges35 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

The click handler can just maintain a timestamp as a JavaScript "Date" instance. Subtract two of those and you have the interval in milliseconds.

Be aware that the clock accuracy is not necessarily that great, and that humans can generate clicks pretty darn fast. Windows, I think, won't give you much better than 15 milliseconds granularity.

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/TxKjT/

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744357385a4570299.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信