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