使用具有不同上下文的回调的removeEventListener()
我正在 PhoneGap 中编写一个移动应用程序,但 Webkit 及其在回调上发生范围上下文更改时从事件列表中删除事件侦听器的能力似乎存在问题。下面是一个示例:
Function.prototype.bind = function(scope) {
var fn = this;
return function () {
fn.apply(scope, arguments);
};
};
a = function(){};
a.prototype.tmp = function(e){
var tmp = ddd.q('#tmp');
tmp.className = 'active';
tmp.addEventListener('webkitAnimationEnd',this.tmp2.bind([this,tmp]),false);
}
a.prototype.tmp2 = function(e){
this[1].removeEventListener('webkitAnimationEnd',this[0].tmp2.bind([this[0],this[1]]),false);
this[1].className = 'inactive;
var t2 = ddd.q('#tmp2');
t2.className = 'active';
t2.addEventListener('webkitAnimationEnd',this[0].setStart.bind([this,t2]),false);
};
现在,在上面的代码中,事件侦听器永远不会剥离,并且每当调用回调时,事件侦听器列表就会变得相当大 - 正如 Web Inspector 中所示。关于如何在使用更改函数范围的回调完成后删除事件侦听器的任何想法?
I'm writing a mobile app in PhoneGap, but there seems to be an issue with Webkit and its ability to remove event listeners from its event list when there's a scope context change on the callback. Below is an example:
Function.prototype.bind = function(scope) {
var fn = this;
return function () {
fn.apply(scope, arguments);
};
};
a = function(){};
a.prototype.tmp = function(e){
var tmp = ddd.q('#tmp');
tmp.className = 'active';
tmp.addEventListener('webkitAnimationEnd',this.tmp2.bind([this,tmp]),false);
}
a.prototype.tmp2 = function(e){
this[1].removeEventListener('webkitAnimationEnd',this[0].tmp2.bind([this[0],this[1]]),false);
this[1].className = 'inactive;
var t2 = ddd.q('#tmp2');
t2.className = 'active';
t2.addEventListener('webkitAnimationEnd',this[0].setStart.bind([this,t2]),false);
};
Now, in the above code, the event listeners never peel off, and whenever the callback gets invoked, the event listener list becomes rather large -- as demonstrated in Web Inspector. Any ideas on how to remove event listeners when they're done using callbacks that change function scope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你能使用像这个jsfiddle示例这样的东西吗?
this
是触发点击事件的对象。self
是A
对象。更新 1 - 第二个示例在这里。主要差异如下。
更新 2 - 一次性事件处理程序的一般示例,绑定您的特定范围的事件处理程序,调用该处理程序,并取消注册侦听器。
Can you use something like this jsfiddle example?
this
is the object on which the click event is fired.self
is theA
object.Update 1 - second example is here. Major differences below.
Update 2 - general example of a one-time event handler that binds your event handler to a particular scope, calls that handler, and unregisters the listener.