Made content state saving more robust [HFJ-1450]

pull/17/head
Paal Joergensen 2015-12-18 10:06:33 +01:00
parent 5e45e7d7dd
commit e83b213183
1 changed files with 22 additions and 9 deletions

View File

@ -1947,8 +1947,14 @@ H5P.createTitle = function (rawTitle, maxLength) {
} }
if (H5PIntegration.saveFreq !== false) { if (H5PIntegration.saveFreq !== false) {
// When was the last state stored
var lastStoredOn = 0;
// Store the current state of the H5P when leaving the page. // Store the current state of the H5P when leaving the page.
var storeCurrentState = function () { var storeCurrentState = function () {
// Make sure at least 250 ms has passed since last save
var currentTime = new Date().getTime();
if (currentTime - lastStoredOn > 250) {
lastStoredOn = currentTime;
for (var i = 0; i < H5P.instances.length; i++) { for (var i = 0; i < H5P.instances.length; i++) {
var instance = H5P.instances[i]; var instance = H5P.instances[i];
if (instance.getCurrentState instanceof Function || if (instance.getCurrentState instanceof Function ||
@ -1960,8 +1966,15 @@ H5P.createTitle = function (rawTitle, maxLength) {
} }
} }
} }
}
}; };
H5P.$window.one('beforeunload unload', storeCurrentState); // iPad does not support beforeunload, therefore using unload
H5P.$window.one('beforeunload unload', function () {
// Only want to do this once
H5P.$window.off('pagehide beforeunload unload');
storeCurrentState();
});
// pagehide is used on iPad when tabs are switched
H5P.$window.on('pagehide', storeCurrentState); H5P.$window.on('pagehide', storeCurrentState);
} }