gpt4 book ai didi

javascript - window.stop() 停止当前加载图像,但加载新图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:47:45 25 4
gpt4 key购买 nike

我有一个脚本,它基于某些事件(用户交互、setTimeout)加载一个 JSON,并根据 JSON 的结果加载一堆图像。我还根据用户与页面的交互预加载了一些图像。有时,在页面打开一段时间后,从 JSON 加载的图像开始“变慢”,需要更多时间来加载(如果我刷新页面,它们加载正常)。所以我开始寻找一种方法来中止当前正在加载的图像,并遇到了“window.stop()”。我阅读了规范中 MDN amd 中的描述,无法理解这是否是我要找的内容。

MDN has :

The stop() method is exactly equivalent to clicking the stop button inthe browser. Because of the order in which scripts are loaded, thestop() method cannot stop the document in which it is contained fromloading, but it will stop the loading of large images, new windows,and other objects whose loading is deferred.

The spec :

The stop() method on Window objects should, if there is an existingattempt to navigate the browsing context and that attempt is notcurrently running the unload a document algorithm, cancel thatnavigation; then, it must abort the active document of the browsingcontext of the Window object on which it was invoked.

abort a document

...Cancel any instances of the fetch algorithm in the context of thisDocument, discarding any tasks queued for them, and discarding anyfurther data received from the network for them...

这是否意味着在使用 window.stop() 后我无法加载更多图像?我做了一些测试,在 Firefox、Chrome 和 Safari 上,这似乎有效(图像在 window.stop 后加载),但这似乎与规范相矛盾。

window.stop();
document.getElementById("c").innerHTML = "<img src='http://a5.mzstatic.com/us/r30/Purple5/v4/5a/2e/e9/5a2ee9b3-8f0e-4f8b-4043-dd3e3ea29766/icon128-2x.png'/>";
<div id="c"></div>

我是否误解了规范,使用这种代码是安全的,还是我应该尝试以不同的方式来做?

最佳答案

是同步调用。在您的代码中,window.stop() 运行,然后加载新图像。规范也很好..

document.getElementById("c").innerHTML = "<img src='//test.com/THIS_WILL_STOP_LOADING.png'/>";
window.stop();
document.getElementById("d").innerHTML = "<img src='//test.com/THIS_WILL_LOAD.png'/>";

关于javascript - window.stop() 停止当前加载图像,但加载新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38309970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com