About the time limit in JavaScript to prevent button repeat clicks _javascript Tips

Source: Internet
Author: User

Front.

One day on a whim, 1 minutes repeatedly clicked on the blog home page Refresh blog list refresh button. Sure enough, IP was disabled at the time. Later, restart your router and retrieve the IP to access the blog's home page. Would it be better to set a limit of time (for example, 1 seconds) to prevent the button from being repeatedly clicked?

Idea of a

The most direct idea may be to click the button, the button's event binding function to bind, 1s after the binding function

<button id= "btn" >0</button>
<script>
btn.onclick = function Add () {
  btn.innerhtml = Number (btn.innerhtml) + 1;  
  Btn.onclick = null;
  Cleartimeout (timer);
  var timer = settimeout (function () {
    Btn.onclick = add;
    },1000);  
}
</script>

Idea Two 

Another idea is to get and record time, and when clicked again, the time interval is greater than 1s.

<button id= "btn" >0</button>
<script>
Btn.onclick = (function () {
  var last = Date.now ();
  return function () {
    var now = Date.now ();
    if ((now-last) >1000) {
      btn.innerhtml= number (btn.innerhtml) + 1;      
    }
    last = Now;
  }
) ();
</script>

The above about JavaScript in the limited time to prevent the button repeatedly click on the idea is small set to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.