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

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

window.location.reload(false); 

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

例如,将所有值重置为默认值,类似于在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;更多的工作a = b;

您可以使用document.location.reload()从缓存重新加载页面。

当然,更好的解决方案是重置变量,但如果没有看到您的代码,我们无法帮助您。