How to measure the time between two button presses?

I am creating a website on which the user repeatedly clicks a button to increase their score. In order not to deceive people, I want to measure the amount of time between each click, and if they click inhumanly quickly, and there is very little time between clicks, I want CAPTCHA or something like that.

How to measure the time between clicks?

+3
source share
2 answers

My suggestion would look like this:

$('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 and save the difference between two clicks in milliseconds. You can use the history array to get the average and check if it's less than 50 ms or something like that.

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

: http://jsfiddle.net/TxKjT/2/

+3

JavaScript "". , .

, , . Windows, , , 15 .

+4

Source: https://habr.com/ru/post/1796491/


All Articles