Turn off elementWatcher?
Closed this issue · 1 comments
anthipapa commented
Hello! I have this function
onExit = () => {
let form = document.querySelector('.the form');
if (form) {
let scrollMonitor = require('scrollmonitor');
let elementWatcher = scrollMonitor.create(form);
elementWatcher.exitViewport(function () {
console.log('I have left the viewport');
});
}
};
which is tied to a componentDidMount one on
window.addEventListener('scroll', this.onExit);
I wanted to know if there is a way to turn of the watcher after just one console.log because now it returns a lot of logs! I saw an issue about an .off options but I couldn't find anything in the documentation. Thanks!!
stutrek commented
Yes, the watcher has a destroy
method, that turns it off permanently. If you want to bring it back after then you can create a new one. you can also elementWatcher.one('exitViewport', () = {})
to run the function once but keep the watcher alive.
https://github.com/stutrek/scrollmonitor/blob/master/README.md#methods