Create a gist now

Instantly share code, notes, and snippets.

@kyo-ago /appcache.js
Last active Oct 28, 2017

What would you like to do?
application cache loading js
(function (app) {
var loading_class = 'loading';
var html = document.documentElement;
loading_class = ' ' + loading_class + ' ';
//未サポート
if (!app || app.UNCACHED === app.status) {
init();
return;
}
//初期読み込み
if (!localStorage.appcaching) {
localStorage.appcaching = 1;
init();
return;
}
//キャッシュが古くなっている
if (app.status === app.UPDATEREADY) {
location.reload();
return;
}
//更新不要
if (app.status === app.IDLE || app.status === app.OBSOLETE) {
init();
return;
}
//ローディング開始
html.className += loading_class;
//更新不要
app.addEventListener('cached', start);
//更新なし
app.addEventListener('noupdate', start);
//更新不可
app.addEventListener('obsolete', start);
//キャッシュが古くなっている
app.addEventListener('updateready', function () {
location.reload();
});
//更新不可
app.addEventListener('error', start);
//timeout
var timeout = setTimeout(start, 3000);
function start () {
if (start.fire) {
return;
}
clearTimeout(timeout);
start.fire = 1;
html.className = html.className.replace(loading_class, '');
init();
}
function init () {
alert('load done.');
}
})(window.applicationCache);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment