javascript 为什么 location.reload() 比其他页面重新加载方法慢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20856899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:28:29  来源:igfitidea点击:

Why location.reload() is slower than the other page reload methods?

javascript

提问by Ionic? Biz?u

Few months ago I posted this answerabout how to refresh the page via JavaScript.

几个月前,我发布了有关如何通过 JavaScript 刷新页面的答案

I provided a JSFIDDLE DEMOtoo:

我也提供了一个JSFIDDLE 演示

var solutions = [
    function () { location.reload(); },
    function () { history.go(0); },
    function () { location.href = location.href; },
    function () { location.href = location.pathname; },
    function () { location.replace(location.pathname); },
    function () { location.reload(false); },
];

$("[data-func]").on("click", function () {
    solutions[parseInt($(this).attr("data-func"))]();
});

Someonenoticed that location.reload()is slower than the other methos. Now I can see the same thing.

有人注意到这location.reload()比其他方法慢。现在我可以看到同样的东西。

Why is it slower? Why the others are faster?

为什么它更慢?为什么其他的更快?

采纳答案by Wildcard27

Been looking for this myself and the best reference I could find is actually on w3schools.com

我自己一直在寻找这个,我能找到的最好的参考实际上是在 w3schools.com

http://www.w3schools.com/jsref/met_loc_reload.asp

http://www.w3schools.com/jsref/met_loc_reload.asp

location.reload(forceGet)

forceGet:

false- Default. Reloads the current page from the cache.

true- The current page must be reloaded from the server

location.reload( forceGet)

强制获取

false- 默认。从缓存中重新加载当前页面。

true- 必须从服务器重新加载当前页面

回答by Panade

From the Mozilla Developement Network I guess the .reloadmethod may fetch all files from the Server again. This would be similar to a CTRL+ F5reload.

从 Mozilla 开发网络我猜该.reload方法可能会再次从服务器获取所有文件。这类似于CTRL+F5重新加载。

The location.hreffor example, simply follows the link which may be cached. As for the MDN definition the behave is not clearly defined so I guess its browser and case specific behave.

location.href,例如,简单地遵循其可被高速缓存的链接。至于 MDN 定义,行为没有明确定义,所以我猜它的浏览器和特定于案例的行为。