Eight Weeks of Prototype: Week 4, Event Handling in Prototype
There are some cases where you will only want to observe an event for a period of time. That is, you want to remove the event handler after some condition is met. To do this, the
stopObserving() method should be used.
Just like when using
observe(), you can call this method directly on an element, or use
Event.stopObserving() and pass the element in question as the first argument. While it doesn't really matter which way you do it, for code consistency you should do it the same as you did when calling
To successfully stop observing the event, you must pass the same arguments to
stopObserving() as you did when you called
observe(). That is, you must pass the event name and the name of the callback function.
Listing 14 shows an example of stopping the observation of an element. In this example, when you click the element an alert box is shown confirming that the event occurred. At this point in the code, the
click event is no longer observed, meaning if you click the element again the alert box will no longer be shown.