visibility-change-ponyfill

visibility-change-ponyfill

A more reliable visibilitychange event that works in more mobile WebViews

Install

1
npm install --save visibility-change-ponyfill
1
import onVisibilityChange from 'visibility-change-ponyfill';

If you don’t use node/babel, include this:

1
<script src="dist/visibility-change-ponyfill.browser.js"></script>

Usage

To add a listener:

1
2
3
4
5
6
7
onVisibilityChange(function () {
if(document.hidden) {
// this page is now hidden
} else {
// this page is now visible
}
});

To remove a listener:

1
2
3
4
function yourOnChangeListener() { /* */ };
onVisibilityChange(yourOnChangeListener);

onVisibilityChange.remove(yourOnChangeListener);

To remove all listeners:

1
onVisibilityChange.removeAll();

License

MIT © Federico Brigante