如何在不刷新页面的情况下重新启动内容

How do I restart content without refreshing the page?

本文关键字:情况下 重新启动 刷新      更新时间:2023-09-26

我正在制作一个快速的JS网络应用程序,我希望能够在不实际重新加载网页的情况下重新启动应用程序。这样,浏览器每次想要重新启动应用程序时都不需要再次获取资源,并且可以在没有互联网访问的情况下重新启动。

将所有

值重置为默认值,类似于您在init中执行的操作,例如

var currentState,    // how things are
    defaultState = { // how things start
        count: 0
    };
function reset() { // make things how they start
    currentState = {};
    for (var key in defaultState) {
        currentState[key] = defaultState[key];
    }
}
function init() { // first time setup
    reset();
}
function count() {
    return ++currentState.count;
}
init(); // prepare everything
// have fun
count(); count(); count(); count(); // 4
// go back
reset();
count(); // 1

当然,如果你正在操作 DOM,重建原始状态将比简单的a = b;涉及更多的工作

您可以使用

document.location.reload()从缓存中重新加载页面。

当然,更好的解决方案是重置变量,但是如果不看到您的代码,我们就无法帮助您解决这个问题。

window.location.reload(false);

false标志将重新执行网站,而无需从本地缓存的 Web 重新加载任何内容。您还可以传递将重新加载所有资源的true