How to temporarily disable a click handler in jQuery?

前端 未结 12 2006
不思量自难忘°
不思量自难忘° 2020-12-04 14:02

Say I have something like the following to trap the click event of a button:

$(\"#button_id\").click(function() {
  //disable click event
  //do something
           


        
12条回答
  •  暖寄归人
    2020-12-04 14:42

    Try utilizing .one()

    var button = $("#button"),
      result = $("#result"),
      buttonHandler = function buttonHandler(e) {
        result.html("processing...");
        $(this).fadeOut(1000, function() {
          // do stuff
          setTimeout(function() {
            // reset `click` event at `button`
            button.fadeIn({
              duration: 500,
              start: function() {
                result.html("done at " + $.now());
              }
            }).one("click", buttonHandler);
    
          }, 5000)
        })
      };
    
    button.one("click", buttonHandler);
    #button {
      width: 50px;
      height: 50px;
      background: olive;
    }
    
    
    click

提交回复
热议问题