简体   繁体   English

检查图像是否已使用 jQuery 缓存

[英]Check if image is already cached with jQuery

I need to add a.load function to an image, however, I do not want this function added if the image is already cached, mainly because in that.load function I fade in the image and hide a loading indicator.我需要将 a.load function 添加到图像中,但是,如果图像已被缓存,我不希望添加此 function,主要是因为在 that.load ZC1C425268E68385D14AB5074C17A 中我淡入了加载指示器并隐藏了加载指示器。

If the image is already cached, I need none of that.如果图像已经被缓存,我不需要这些。 Is there a way to check if it's in the cache?有没有办法检查它是否在缓存中?

Thanks, Wesley谢谢,韦斯利

I appreciate the question was asked a little while ago but I've been looking to solve the same issue, and in case someone else comes across this, here is how I solved it.我很欣赏这个问题是不久前提出的,但我一直在寻求解决同样的问题,如果其他人遇到这个问题,这就是我解决它的方法。

I used a window.setTimeout() with 200ms delay which called my preloader effect, and in the.load() event for the image being loaded, the first thing it does is clear the timeout, so if the load was quick - no effect is shown, but if the image load takes longer than 200ms the preloader is displayed:我使用了具有 200 毫秒延迟的 window.setTimeout() 调用了我的预加载器效果,并且在加载图像的 .load() 事件中,它所做的第一件事就是清除超时,所以如果加载很快 - 没有效果显示,但如果图像加载时间超过 200 毫秒,则会显示预加载器:

var imgTmr;

imgTmr = window.setTimeout(function() {showLoading('zoomImage');}, 200);

$("#zoomImage").load(function() {
    window.clearTimeout(imgTmr);
    hideLoading();
}

$("#zoomImage").attr("src", "myLargeImage.jpg");

// The showLoading() and hideLoading() functions just display a wait graphic 
// the image being loaded in this instance has the id of zoomImage

Typically I just leave the load event in place.通常我只是将加载事件留在原处。 Even when the image is cached, it takes a few milliseconds (depending on size and computer processing speed) to load it from your computer.即使图像被缓存,从您的计算机加载它也需要几毫秒(取决于大小和计算机处理速度)。 Loading still takes place just not from the same source.加载仍然发生,只是不是来自同一来源。 Therefore you should have the loading event tied to it if it's cached or not.因此,无论是否缓存,您都应该将加载事件绑定到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM