admin管理员组文章数量:1331913
In this module I'm working on I have a listener to a 'resize' event in the window. Every time the module is ran, I need to check if there's already a listener registered to that event and detach it, in order to avoid unwanted behavior, memory leaks and etc.
So far so good, but, in this application we are working on, chances are that some handlers are already attached to the 'resize' event and I can't call $(window).off('resize')
, as this would flush all the other event handlers previously registered by other plugins or modules.
Being that said, I would like to know if there's a way to identify my handler and only detach things that I have registered myself. How do I set an identifier to my event handler in order to be referenced in the .off()
function?
Any help would be nice.
In this module I'm working on I have a listener to a 'resize' event in the window. Every time the module is ran, I need to check if there's already a listener registered to that event and detach it, in order to avoid unwanted behavior, memory leaks and etc.
So far so good, but, in this application we are working on, chances are that some handlers are already attached to the 'resize' event and I can't call $(window).off('resize')
, as this would flush all the other event handlers previously registered by other plugins or modules.
Being that said, I would like to know if there's a way to identify my handler and only detach things that I have registered myself. How do I set an identifier to my event handler in order to be referenced in the .off()
function?
Any help would be nice.
Share edited Sep 23, 2022 at 17:58 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 9, 2014 at 17:34 darksoulsongdarksoulsong 15.3k14 gold badges51 silver badges96 bronze badges 3- 1 use off to remove all handlers and just write new event after it.. – Ehsan Sajjad Commented May 9, 2014 at 17:36
- 2 use namespaced event to bind/unbind it or use a referenced handler – A. Wolff Commented May 9, 2014 at 17:37
- @EhsanSajjad I can't do that because I would remove the other handlers this way. I need to remove only the event handlers that I have attached myself; – darksoulsong Commented May 9, 2014 at 17:38
3 Answers
Reset to default 11Here 2 different suggestions. You can either give a namespace to your event or pass a none anonymous function when binding.
Example when using namespace :
$(window).on('resize.event1', function(){});
$(window).on('resize.event2', function(){});
//Only turn off event 2
$(window).off('resize.event2');
jQuery allow you to identify your event. This behaviour is called namespace. The current event and the event name must be separate by a dot. In that case, i am binding 2 resize event (left side of the dot), one with the name event1
and the other with event2
.
By identifying the event, you can remove (or trigger) single event when an object have multiple of the same event.
$(window).trigger('resize'); //Trigger both;
$(window).trigger('resize.event2'); //Trigger event2;
Example using named functions :
$(window).on('resize', function1);
$(window).on('resize', function2);
//Only turn off event 2
$(window).off('resize', function2);
Associating a none anonymous function allow you to remove the event if the passed function match the current event function. Because function2 === function2
, only the second function will be remove since function1 !== function2
!
If you use named handlers (as opposed to anonymous functions) you can pass the name of the handler to off
to remove just that one (http://api.jquery./off/)
function flash() {
$( "div" ).show().fadeOut( "slow" );
}
$( "body" ).off( "click", "#theone", flash )
Do something like this-
$(window).off('resize.myCustomEvent');
and vice versa to attach it
本文标签: javascriptMultiple Handlers attached to one event How to detach only oneStack Overflow
版权声明:本文标题:javascript - Multiple Handlers attached to one event: How to detach only one? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742264923a2443208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论