简体   繁体   中英

Javascript Disable button and reenable it after 5 seconds

I want to disable my button on click and then reenable in after 5 seconds but its not working properly.

  function submitPoll(id){
      document.getElementById("votebutton").disabled = true;
      document.getElementById("votebutton").delay(5000).disabled = false;
  }

.delay is jQuery. You can simply use setTimeout in Javascript.

function submitPoll(id){
      document.getElementById("votebutton").disabled = true;
      setTimeout(function(){document.getElementById("votebutton").disabled = false;},5000);
  }

Like Zee said .delay is a JQuery function which can only be used on a JQuery object. Currently you are only using a reference to an HTML element which is produced by .getElementById()

If you are wanting a button that disables itself. I went ahead and made a little example on jsfiddle.

http://jsfiddle.net/1z0bx0t7/

Using code that looks like:

function submitPoll() {
    document.getElementById("votebutton").disabled = true;
    setTimeout(function() {
        document.getElementById("votebutton").disabled = false;
    }, 5000);
}
document.getElementById("votebutton").addEventListener("click", submitPoll);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM