最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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/

发布评论

评论列表(0)

  1. 暂无评论