How do i remove a listener for an event I've previously registered to with the new dart library?
I'm using this to listen for events with the new library:
element.onMouseMove.listen
What is the equivalent version of this?
element.on.mouseMove.remove
JavaScript | removeEventListener() method with examples The removeEventListener() is an inbuilt function in JavaScript which removes an event handler from an element for a attached event. for example, if a button is disabled after one click you can use removeEventListener() to remove a click event listener.
If there is no memory leak, the used memory will increase by around 1000kb or less after the tests are run. However, if there is a memory leak, the memory will increase by about 16,000kb. Removing the event listener first always results in lower memory usage (no leaks).
Since we only need the listener for our modal, it should be removed whenever the user cannot interact with our modal any longer. The same is true for any element that can be toggled as well as animations on elements.
According to the jquery Documentation when using remove() method over an element, all event listeners are removed from memory. This affects the element it selft and all child nodes. If you want to keep the event listners in memory you should use . detach() instead.
element.onMouseMove.listen(...)
returns a StreamSubscription. Simply call its cancel() method to stop receiving events.
In addition to Alexandre's answer, a concrete example in code would look like this:
import 'dart:html';
void main() {
var listener = (e) => print("Moved");
var element = query("#text");
var streamsub = element.onMouseMove.listen(listener); // <--- add the listener
var reverseText= (Event event) {
var text = query("#text").text;
var buffer = new StringBuffer();
for (int i = text.length - 1; i >= 0; i--) {
buffer.add(text[i]);
}
query("#text").text = buffer.toString();
streamsub.cancel(); // <---- stop listening
};
element
..text = "Click me!"
..onClick.listen(reverseText);
}
This prints "Moved" when you move the mouse over the "Click Me!" text (from the standard dart sample). When clicked, it stops.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With