如果正在更新应用程序缓存,则阻止ajax调用 [英] prevent ajax call if application cache is being updated

查看:187
本文介绍了如果正在更新应用程序缓存,则阻止ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脱机Web应用程序.

i am writing an offline web application.

我有一个用于我的applicationCache的清单文件.我有appCache事件的处理程序. 我想检测正在下载的文件是第一次下载还是正在更新.因为如果要更新它们,我想阻止我的应用程序代码运行,因为我将在更新我的应用程序后刷新浏览器.

i have a manifest files for my applicationCache. and i have handlers for appCache events. i want to detect if the files being downloaded are being downloaded for the first time or being updated. because in case they are being updated, i would like prevent my application code from running, since i will refresh the browser after updating my app.

我的具体问题是,在触发检查"事件时,applicationCache状态已经为下载中",

my specific problem here is that when the "checking" events gets fired, the applicationCache status is already "DOWNLOADING",

有人知道在下载任何清单或文件之前如何获取applicationCache.status吗?

does anybody know how to get the applicationCache.status before any manifest or files gets downloaded?

提前感谢您的回答

window.applicationCache.addEventListener('checking', function (event) {
    console.log("Checking for updates.");
    console.log("inside checking event, appcache status : %s", applicationCache.status);
}, false);

window.applicationCache.addEventListener('updateready', function (e) {
    if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
        // Browser downloaded a new version of manifest files
        window.location.reload();
    }
}, false);

window.applicationCache.addEventListener('downloading', function (event) {
    appCommon.information("New version available", "Updating application files...", null, null);
}, false);

window.applicationCache.addEventListener('progress', function (event) {
    $("#informationModal").find(".modal-body").html("Updating application files... " + event.loaded.toString() + " of " + event.total.toString());
}, false);

window.applicationCache.addEventListener('cached', function (event) {
    $("#informationModal").find(".modal-body").html("Application up to date.");
    setTimeout(function () { $("#informationModal").find(".close").click(); }, 1000);
}, false);

推荐答案

根据

According to the specification the checking event is always the first event.

用户代理正在检查更新,或尝试首次下载清单. 这始终是序列中的第一个事件.

我认为刷新浏览器是错误的,因为这将不会自动获取新副本.如果您查看 MDN指南,以了解如何使用applicationCache,您将看到始终总是先从applicationCache加载文件,然后继续进行尝试通过刷新避免发生的事件.

I think it is a mistake to refresh the browser since that will not automatically fetch a new copy. If you look at the MDN guide for using the applicationCache you'll see that files are always loaded from the applicationCache first and then proceed to go through the cycle of events you attempted to avoid by refreshing.

代替刷新,您应该简单地充分利用applicationCache事件生命周期来初始化和启动您的应用程序.

Instead of refreshing you should simply make proper use of the applicationCache event life cycle in order to initialize and start your application.

我想阻止我的应用程序代码运行,因为我将在更新应用程序后刷新浏览器.

i would like prevent my application code from running, since i will refresh the browser after updating my app.

您可以控制应用程序何时开始运行,如果您确实要刷新浏览器,只需始终在updatereadynoupdate事件之后启动应用程序,但实际上并没有刷新它的感觉应该使用 window.applicationCache.swapCache .

You have a lot of control of when your application begins to run, if you really wanted to refresh the browser, just always start the app after updateready or noupdate events, but really instead of refreshing it seems like you should use window.applicationCache.swapCache instead.

我发现此问题和答案有用

这篇关于如果正在更新应用程序缓存,则阻止ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆