Calculating the time between two clicks in Javascript

javascript, jquery

Solution

Something like this would do the trick. Keep a variable with the time of the last click and then compare it when the user clicks the link again. If the difference is < 5 seconds show the alert

<a id='testLink' href="#">click here</a>
<script type='text/javascript'>
    var lastClick = 0;
    $("#testLink").click(function() {
        var d = new Date();
        var t = d.getTime();
        if(t - lastClick < 5000) {
             alert("LESS THAN 5 SECONDS!!!");
        }
        lastClick = t;
    });
</script>

Problem

I want to calculate the time between two clicks of an attribute with javascript but I don't know how. For example; ``` <a href="#">click here</a> ``` if the user clicks more than once -let's say in 5 seconds- I want to display an alert. I'm using jQuery if that helps. I don't know much about javascript but I've been coding a small project in my free time.

Original source