Front-end prevent button from being clicked multiple times

Source: Internet
Author: User

Some front-end logics sometimes control front-end display, such as recording the number of favorites. Sometimes repeated clicks may cause a front-end display bug. Therefore, some logic judgment is required to remove repeated clicks.

The implementation part of the code is as follows, mainly through setTimeout to determine, that is, no matter the number of clicks, a certain period of time will trigger an event, so that only one record is generated:

<SCRIPT> var I = 0; // determine the number of clicks stored var closetimer = NULL; // delay function storage function button#click () // botton Click Event {console. log ('1'); I ++; // record the number of clicks closetimer = Window. setTimeout (setout, 200);} function setout () {// click the execution event if (I> 1) // if the number of clicks exceeds 1 {console. log ('wrong '); window. cleartimeout (closetimer); // clear the delay function closetimer = NULL; // set the delay storage to null // Add the operation code I = 0; // reset the number of clicks to 0} else {// if the number of clicks is 1 Console. log ('right'); I = 0; // reset the number of clicks to 0 // Add the code to execute the operation }}</SCRIPT>


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.