在Javascript中计算两次单击之间的时间

时间:2023-01-27 21:31:26

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

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

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.

如果用户点击不止一次——比方说5秒后——我想显示一个警告。如果有用的话,我使用jQuery。我不太了解javascript,但我在空闲时间编写了一个小项目。

3 个解决方案

#1


9  

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

像这样的东西就可以了。在最后一次单击时保留一个变量,然后在用户再次单击链接时进行比较。如果差异小于5秒,则显示警告

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

#2


1  

The following may help you getting started:

下面这些可能会帮助你开始:

var lastClicked = 0;

function onClickCheck() {
    var timeNow = (new Date()).getTime();

    if (timeNow > (lastClicked + 5000)) {
        // Execute the link action
    }
    else {
        alert('Please wait at least 5 seconds between clicks!');
    }

    lastClicked = timeNow;
}

HTML:

HTML:

<a href="#" onClick="onClickCheck();">click here</a>

#3


0  

  1. Create a variable to hold the time of a click, say lastClick.
  2. 创建一个变量来保存单击的时间,例如lastClick。
  3. Set up a click handler for the element you want to track clicks on.
  4. 为要跟踪单击的元素设置单击处理程序。
  5. Inside the handler, check for a value in lastClick. If there is no value, set it to the current time. If there is a value, compare it against the current time. If the difference is within the range you're checking for, display the alert.
  6. 在处理程序内部,检查lastClick中的值。如果没有值,则将其设置为当前时间。如果有一个值,将它与当前时间进行比较。如果差异在您要检查的范围内,请显示警报。

#1


9  

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

像这样的东西就可以了。在最后一次单击时保留一个变量,然后在用户再次单击链接时进行比较。如果差异小于5秒,则显示警告

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

#2


1  

The following may help you getting started:

下面这些可能会帮助你开始:

var lastClicked = 0;

function onClickCheck() {
    var timeNow = (new Date()).getTime();

    if (timeNow > (lastClicked + 5000)) {
        // Execute the link action
    }
    else {
        alert('Please wait at least 5 seconds between clicks!');
    }

    lastClicked = timeNow;
}

HTML:

HTML:

<a href="#" onClick="onClickCheck();">click here</a>

#3


0  

  1. Create a variable to hold the time of a click, say lastClick.
  2. 创建一个变量来保存单击的时间,例如lastClick。
  3. Set up a click handler for the element you want to track clicks on.
  4. 为要跟踪单击的元素设置单击处理程序。
  5. Inside the handler, check for a value in lastClick. If there is no value, set it to the current time. If there is a value, compare it against the current time. If the difference is within the range you're checking for, display the alert.
  6. 在处理程序内部,检查lastClick中的值。如果没有值,则将其设置为当前时间。如果有一个值,将它与当前时间进行比较。如果差异在您要检查的范围内,请显示警报。