mirror of
https://github.com/locomotivemtl/locomotive-boilerplate.git
synced 2026-01-15 00:55:08 +08:00
Testing addition of pseudo page visibility API
This commit is contained in:
36
assets/scripts/utils/visibility.js
Normal file
36
assets/scripts/utils/visibility.js
Normal file
@@ -0,0 +1,36 @@
|
||||
/* jshint esnext: true */
|
||||
import { $document, $window, $html, $body } from '../utils/environment';
|
||||
|
||||
const CALLBACKS = {
|
||||
hidden: [],
|
||||
visible: []
|
||||
};
|
||||
|
||||
// Main event
|
||||
$document.on('visibilitychange', function(event) {
|
||||
if(document.hidden){
|
||||
onDocumentChange('hidden');
|
||||
}else{
|
||||
onDocumentChange('visible');
|
||||
}
|
||||
});
|
||||
|
||||
function registerDocumentHiddenCallback(callback) {
|
||||
CALLBACKS['hidden'].push(callback);
|
||||
}
|
||||
|
||||
function registerDocumentVisibleCallback(callback) {
|
||||
CALLBACKS['visible'].push(callback);
|
||||
}
|
||||
|
||||
function onDocumentChange(state) {
|
||||
let callbacks = CALLBACKS[state];
|
||||
let i = 0;
|
||||
let len = callbacks.length;
|
||||
|
||||
for (; i < len; i++) {
|
||||
callbacks[i]();
|
||||
}
|
||||
}
|
||||
|
||||
export {registerDocumentHiddenCallback, registerDocumentVisibleCallback};
|
||||
Reference in New Issue
Block a user