计算Javascript中两次点击之间的时间 [英] Calculating the time between two clicks in Javascript

查看:106
本文介绍了计算Javascript中两次点击之间的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用javascript计算两次点击属性之间的时间,但我不知道如何。

I want to calculate the time between two clicks of an attribute with javascript but I don't know how.

例如;

<a href="#">click here</a>

如果用户点击多次-let说5秒钟 - 我想显示提醒。如果有帮助我正在使用jQuery。我对javascript了解不多,但我在空闲时间编写了一个小项目。

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.

推荐答案

这样的东西会做的伎俩。保留变量与最后一次点击的时间,然后在用户再次单击链接时进行比较。如果差异<< 5秒显示提醒

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>

这篇关于计算Javascript中两次点击之间的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆