jQuery 图像预加载/缓存停止浏览器
简而言之,我有一个非常大的照片库,并且我试图在加载第一页时缓存尽可能多的缩略图。可能有 1000 多个缩略图。
第一个问题——尝试预加载/缓存那么多是不是很愚蠢?
第二个问题——当 preload()
函数触发时,整个浏览器会停止响应一到两分钟。此时会触发回调,因此预加载已完成。有没有一种方法可以实现“智能预加载”,在尝试加载这么多对象时不会影响用户体验/速度?
$.preLoadImages
函数取自此处:http://binarykitten.me.uk/dev/jq-plugins/107-jquery-image-preloader-plus-callbacks.html
这是我的实现方式:
$(document).ready(function() {
setTimeout("preload()", 5000);
});
function preload() {
var images = ['image1.jpg', ... 'image1000.jpg'];
$.preLoadImages(images, function() { alert('done'); });
}
1000图片很多。我是不是要求太多了?
In short, I have a very large photo gallery and I'm trying to cache as many of the thumbnail images as I can when the first page loads. There could be 1000+ thumbnails.
First question -- is it stupid to try to preload/cache that many?
Second question -- when the preload()
function fires, the entire browser stops responding for a minute to two. At which time the callback fires, so the preload is complete. Is there a way to accomplish "smart preloading" that doesn't impede on the user experience/speed when attempting to load this many objects?
The $.preLoadImages
function is take from here: http://binarykitten.me.uk/dev/jq-plugins/107-jquery-image-preloader-plus-callbacks.html
Here's how I'm implementing it:
$(document).ready(function() {
setTimeout("preload()", 5000);
});
function preload() {
var images = ['image1.jpg', ... 'image1000.jpg'];
$.preLoadImages(images, function() { alert('done'); });
}
1000 images is a lot. Am I asking too much?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看该预加载脚本后,该脚本似乎不会等待一个图像加载就继续处理下一个图像。我相信这就是导致您的浏览器挂起的原因 - 您实际上是在告诉浏览器同时加载一百张图像。
更好的方法是使用递归函数仅在第一个图像完成后才开始加载下一个图像。这是我整理的一个简单的例子。
编辑:这会导致堆栈溢出错误,请参阅下面的新版本。
同样,这里重要的是确保您只强制浏览器一次下载一张图像。除此之外,您可能会面临锁定浏览器的风险,直到它们全部完成。
--
编辑: 新版本没有递归。我用 1000 多个项目数组测试了这个,没有遇到任何错误。我的想法是用间隔和布尔值替换递归;每 50 毫秒,我们会轮询该函数并询问“正在加载任何内容吗?”如果答案是否定的,我们将继续排队下一张图像。这个过程一遍又一遍地重复,直到全部完成。
我仍然很好奇,在包含许多其他内容的完整网页上,从性能角度来看,这将如何表现。让我们知道进展如何。
After looking over that preload script, it appears that the script does not wait for one image to load before moving on to the next. I believe this is what causes your browser to hang - you're essentially telling the browser to load a hundred images all at the same time.
A better way would be to use a recursive function to start loading the next image only after the first one is done. Here is a simple example I put together.
Edit: This one causes stack overflow errors, see new version below.
Again, the important thing here is to make sure you are only forcing the browser to download one image at a time. Any more than that and you risk locking the browser up until they're all finished.
--
Edit: New version sans recursion. I tested this one with a 1000+ item array and didn't encounter any errors. My idea was to replace the recursion with an interval and a boolean; Every 50 milliseconds, we poll the function and and ask "anything loading?" If the answer is no, we'll go ahead and queue up the next image. This repeats over and over until its all done.
I'm still curious how this will do, performance wise, on a full webpage with lots of other stuff going on. Let us know how it goes.
这是一个有趣的性能问题。当您预加载所有内容时,它在 Firebug 或 Chrome 开发人员工具中的执行情况如何?这让我想到这将是延迟加载插件的一个很好的用途。
This is an interesting performance question. How does it perform in Firebug or the Chrome Developer Tools when you preload it all? This makes me think of is that this would be a good use of the Lazy Load Plugin.