LoginSignup
1
1

More than 5 years have passed since last update.

jQueryでonしてすぐにoffする場合。

Posted at
allFunciton.js
$('#html_id').on('any_event', function(e){
  $(this).off('any_event');
});

もちろんこれでもいいのだが、他のイベントリスナーまでオフにしてしまう。

なのでe.handleObj.handlerを使って解決する。

selfFuncitonOnly.js
$('#html_id').on('any_event', function(e){
  $(this).off('any_event', e.handleObj.handler);
});

上記、jQueryなのでブラウザテストしてないです!!!

以下でも行ける気がするのだが、どうなんだろう。

useFunction.js
handlerFunction = function(e){
  $(this).off('any_event', handlerFunction);
}
$('#html_id').on('any_event', handlerFunction);

評価順的に混乱しそうなのでやってないです・・・・

1
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1