JavaScript code that invalidates the button for 5 seconds

Source: Internet
Author: User

First, give a good code:
<Input type = "button" value = "valid for 5 seconds" onclick = "setTimeout (function (me) {me. disabled = true; return function () {me. disabled = false ;};}) (this), 5000); ">
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

The second option is also good.
<Input type = "button" value = "5 seconds after expiration" onclick = "{this. disabled = true; var me = this; setTimeout (function () {me. disabled = false;}, 5000);} ">
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The third method is simplified.
<Input type = "button" value = "valid for 5 seconds" id = "me" onclick = "this. disabled = true; setTimeout ('me. disabled = false ', 5000); ">
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The key is that this in the first setTimeout parameter is window, so a more practical variable is required.
Directly relying on this in setTimeout is not feasible, because it points to the window, which can be implemented through the vertices to be processed.
Fengyan's method is to pass in the correct this reference before setTimeout executes the countdown, and return a required packet with the correct node reference
It is the execution function after the countdown of setTimeout. The parameter name is me or Mes.

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.