On Mon, Nov 9, 2009 at 3:36 PM, Karen <karent...@gmail.com> wrote:
>
> I looked in the library that removeListener is supported but am not
> fully sure how to use it.
>
> I tried using "this", but that doesn't seem to successfully remove the
> listener. What am I missing here?
>
>        chrome.extension.onConnect.addListener( function( port ) {
>
>                if (port.name == "foo") {
>                        port.onMessage.addListener(function(msg) {
>
>                                chrome.extension.connect( {name:
> "foo"} ).onMessage.removeListener( this );
>                                ....
>                         }
>                }
>          }

Good question. You need a reference to the function. |this| won't do
what you want, it returns a reference to the object the currently
executing function was called as a method of. In this case, there is
no such object, so it just returns the global object.

If you are inside a function, and want a reference to that function,
you can always use arguments.callee, like this:

port.onMessage.addListener(function() {
  port.onMessage.removeListener(arguments.callee);
});

Another way to do it is to give the function a name, then you can
refer to it by that name:

function listener() {
  port.onMessage.removeListener(arguments.callee);
}
port.onMessage.addListener(listener);

HTH,

- a

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Chromium-extensions" group.
To post to this group, send email to chromium-extensions@googlegroups.com
To unsubscribe from this group, send email to 
chromium-extensions+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/chromium-extensions?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to