The idea of preventing buttons from repeating clicks within a time limit in JavaScript

Source: Internet
Author: User

Previous words

One day a whim, 1 minutes repeatedly clicked the blog home page of the refresh post list of the Refresh button. Sure enough, IP was disabled at the time. Later, restart your router, regain the IP to access the blog Park home page. Is it better to set a time limit (say 1 seconds) to prevent the button from being repeatedly clicked?

Idea One

The most straightforward idea may be that the button's event binding function is unbound after the button is clicked, and the function is re-bound after 1s

<button id= "btn" >0</button><script>function  Add () {    = number ( btn.innerhtml) + 1;         NULL ;    Cleartimeout (timer);     var timer = setTimeout (function() {        = add;        },+);    } </script>

Two ideas

Another idea is to acquire and record time, which is valid when the time interval is greater than 1s when you click again

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

If there are other ideas, please contact

The idea of preventing buttons from repeating clicks within a time limit in JavaScript

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.