javascript 预加载图像 - 检查图像是否被缓存/加载以防止预加载

发布于 2024-09-27 02:14:16 字数 584 浏览 0 评论 0原文

我有一些疑问,我已经确定,但仍然想非常精确。

如果这是重复的问题,请给我链接,以便我删除这个问题并将引用提供的链接

  1. 我需要预加载一些图像。如果它们已存在于浏览器缓存中,我不想预加载。

  2. 如果我们要预加载,那么我们使用 JavaScript 代码来预加载发送请求。 我们在 html 文档中也有相同图像的 img src。浏览器将请求图像两次。一个是在预加载时,另一个是在看到 img 标签时?

  3. 我希望预加载和img src进程都是异步的。我希望链接像 obj.src = path 这样的图像时所采取的资源和请求是相同的。

  4. 我希望预加载不会像同步 Ajax 那样工作。也就是说,通过给予优先级

  5. 是否可以知道图像是否在缓存中,以便我们不必预加载它。

  6. 我还漏掉了几个问题吗?如果是这样,那么请提供这些答案,以便堆栈溢出用户可以更新。

只要是或否就好了。如果可能的话,我希望有一些外部参考或建议或答案的链接。

谢谢。

I have a few doubts which i am already sure of it but still want to be very precise about it.

If this is a duplicate question then please give me links so that i will delete this question and will refer the provided links

  1. I need to preload few images. I dont want to preload if they already exists in the browser cache.

  2. if we are preloading then we use the javascript code to preload which sends a request.
    also we have an img src for the same image in the html document. will the browser request for an image two times. one while preloading and other when it sees the img tag?

  3. I hope both the preloading and img src processes are asynchronous. i hope that the resource and request taken when linking an image like obj.src = path and will be the same.

  4. i hope preloading will not work like synchronous Ajax. that is by giving priority

  5. is it possible to know whether an image is in cache so that we dont have to preload it.

  6. am i missing few more questions? if so then please provide those answers so that the stack overflow users could update.

Just yes or no would be nice. if possible i would like to have some links to external reference or suggestions or an answser.

Thank You.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-10-04 02:14:16

您可以检查 .complete 属性,如果图像已缓存,它应该大部分都是正确的。

如果您在缓存它们时预加载它们,则不会导致太多网络流量。您是否想节省内存?

您可以通过 ajax 执行 head 请求,如果它返回 304 而不是 200,则意味着它在缓存中。

关于参考资料:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

You can check the .complete attribute, it should mostly be true is image is cached.

If you preload them when they are cached, its not going to cause much network traffic.. Are you trying to save on memory?

You can do a head request via ajax and if it returns 304 instead of 200, it would mean its there in the cache.

Regarding References :
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

羁客 2024-10-04 02:14:16
var preloadImgs = function(){
    var current, image_urls = ['image_one.jpg', 'image_two.jpg', 'image_three.jpg'], i, imgObj = new Image;

    for (i = 0; i < image_urls.length; i += 1) {
        current = (imgObj.src = image_urls[i]);
        if (current.complete) { // image is cached/loaded
          // do something with the cached/loaded image
        }
    }
};

“我需要预加载一些图像。如果它们已存在于浏览器缓存中,我不想预加载”。 没关系,预加载是为图像对象的 src 属性分配一个 url,然后它会完成其余的工作,如果图像已经可用,则请求下一个图像。

var preloadImgs = function(){
    var current, image_urls = ['image_one.jpg', 'image_two.jpg', 'image_three.jpg'], i, imgObj = new Image;

    for (i = 0; i < image_urls.length; i += 1) {
        current = (imgObj.src = image_urls[i]);
        if (current.complete) { // image is cached/loaded
          // do something with the cached/loaded image
        }
    }
};

"I need to preload few images. I dont want to preload if they already exists in the browser cache". It does not matter, preloading is assinging a url for the src property of the image object and it does the rest, if the image is already availble the next image is requested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文