How to temporarily disable a click handler in jQuery?

前端 未结 12 2029
不思量自难忘°
不思量自难忘° 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:37

    This is a more idiomatic alternative to the artificial state variable solutions:

    $("#button_id").one('click', DoSomething);
    
    function DoSomething() {
      // do something.
    
      $("#button_id").one('click', DoSomething);
    }
    

    One will only execute once (until attached again). More info here: http://docs.jquery.com/Events/one

提交回复
热议问题