jQuery 图像预加载/缓存停止浏览器

发布于 2024-09-06 00:55:52 字数 751 浏览 7 评论 0原文

简而言之,我有一个非常大的照片库,并且我试图在加载第一页时缓存尽可能多的缩略图。可能有 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 技术交流群。

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

发布评论

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

评论(2

你好,陌生人 2024-09-13 00:55:52

查看该预加载脚本后,该脚本似乎不会等待一个图像加载就继续处理下一个图像。我相信这就是导致您的浏览器挂起的原因 - 您实际上是在告诉浏览器同时加载一百张图像。

更好的方法是使用递归函数仅在第一个图像完成后才开始加载下一个图像。这是我整理的一个简单的例子。

编辑:这会导致堆栈溢出错误,请参阅下面的新版本。

var preLoadImages = function(imageList, callback) {
    var count = 0;
    var loadNext = function(){
        var pic = new Image();
        pic.onload = function(){
            count++;
            if(count >= imageList.length){
                callback();
            }
            else{
                loadNext();
            }
        }
        pic.src = imageList[count];
    }
    loadNext();
};

$(document).ready(function(){
    var files = ['file1,jpg', 'file2.jpg'];
    preLoadImages(files, function(){ alert("Done!"); });
});

同样,这里重要的是确保您只强制浏览器一次下载一张图像。除此之外,您可能会面临锁定浏览器的风险,直到它们全部完成。

--

编辑: 新版本没有递归。我用 1000 多个项目数组测试了这个,没有遇到任何错误。我的想法是用间隔和布尔值替换递归;每 50 毫秒,我们会轮询该函数并询问“正在加载任何内容吗?”如果答案是否定的,我们将继续排队下一张图像。这个过程一遍又一遍地重复,直到全部完成。

var preLoadImages = function(imageList, callback) {
    var count = 0;
    var loading = false;
    var loadNext = function(){
        if(!loading){
            loading = true;
            count++;
            pic = new Image();
            pic.onload = function(){
                loading = false;
                if(count >= imageList.length-1){
                        clearInterval(loadInterval);
                        callback();
                }
            }
            pic.src = imageList[count];
        }
    }
    var loadInterval = window.setInterval(loadNext, 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.

var preLoadImages = function(imageList, callback) {
    var count = 0;
    var loadNext = function(){
        var pic = new Image();
        pic.onload = function(){
            count++;
            if(count >= imageList.length){
                callback();
            }
            else{
                loadNext();
            }
        }
        pic.src = imageList[count];
    }
    loadNext();
};

$(document).ready(function(){
    var files = ['file1,jpg', 'file2.jpg'];
    preLoadImages(files, function(){ alert("Done!"); });
});

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.

var preLoadImages = function(imageList, callback) {
    var count = 0;
    var loading = false;
    var loadNext = function(){
        if(!loading){
            loading = true;
            count++;
            pic = new Image();
            pic.onload = function(){
                loading = false;
                if(count >= imageList.length-1){
                        clearInterval(loadInterval);
                        callback();
                }
            }
            pic.src = imageList[count];
        }
    }
    var loadInterval = window.setInterval(loadNext, 50);
};

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.

自由如风 2024-09-13 00:55:52

这是一个有趣的性能问题。当您预加载所有内容时,它在 Firebug 或 Chrome 开发人员工具中的执行情况如何?这让我想到这将是延迟加载插件的一个很好的用途。

Lazy loader 是一个 jQuery 编写的插件
在 JavaScript 中。它延迟加载
(长)网页中的图像。图片
视口之外(可见部分
网页)不会在用户之前加载
滚动到他们。这与
图像预加载。

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.

Lazy loader is a jQuery plugin written
in JavaScript. It delays loading of
images in (long) web pages. Images
outside of viewport (visible part of
web page) wont be loaded before user
scrolls to them. This is opposite of
image preloading.

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