Skip to content

Instantly share code, notes, and snippets.

@kyo-ago
Last active October 28, 2017 13:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyo-ago/4160931 to your computer and use it in GitHub Desktop.
Save kyo-ago/4160931 to your computer and use it in GitHub Desktop.
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