简体   繁体   English

使用jQuery重新加载多个图像? 如何优化?

[英]Reload multiple images using jQuery? How to optimize?

I have an update of multiple images using jQuery: 我使用jQuery更新了多个图像:

window.onload = function () {
    setInterval(function () {
        $(".image").each(function (index) {
            base_url = $(this).attr('src').split('?rand=')[0];
            address = base_url + '?rand=' + Math.random();
            $(this).attr("src", address);
        });
    }, 10000);
};

I'm trying to improve it: 我正在尝试改善它:

function update() {
    setInterval(function () {
        $(".cameragrid_").find("img").each(function (index) {
            d = new Date();
            var src = $(this)[0].src;
            var state = $(this).context.readyState;
            if (state == 'complete'); {
                $(this).attr("src", src + d.getTime());
            }
        });
        console.log('click');
    }, 10000);
}

$(window).ready(function () {
    $('.cameragrid_').hide();
});

$(window).load(function () {
    $('.cameragrid_').show();
    update();
});

I wanted to reduce the time from 10 to 3 seconds, but when I reduce this time, do not update all the images, my algorithm and the rest is not updated. 我希望将时间从10秒减少到3秒,但是当我减少此时间时,不要更新所有图像,我的算法和其余图像都不会更新。 .

Is there any way to optimize it to run within 3 seconds ? 有什么方法可以优化它在3秒内运行?

Hey have you tried https://tinypng.com/ this compress the image format, this will increase some loading time of image more over you can also work on making the sprite of 10 images into 1 in this way loading only one file and then indexing the sprite. 嘿,您尝试过https://tinypng.com/压缩图像格式,这会增加图像的加载时间,您还可以将10张图像的精灵以这种方式加载为1个,然后仅加载一个文件,然后索引精灵。

Never personally tried image-min but this is also worth looking. 从来没有亲自尝试过最小图像,但这也值得一看。

Believe sprite could solve your problem in shot. 相信精灵可以解决您的问题。

instead of having one timer for many images maybe you could try to have many timers, one for each image or at least a small batch of images. 而不是为多个图像使用一个计时器,您可以尝试使用许多计时器,每个图像使用一个计时器,或者至少一小部分图像。

window.onload = function () {

    $(".image").each(function (index) {
        setInterval(function () {
             base_url = $(this).attr('src').split('?rand=')[0];
             address = base_url + '?rand=' + Math.random();
             $(this).attr("src", address);
        }, 3000);
    });

};

I've not tested this code but you can get the idea, maybe you'll have to add a $.proxy() somewhere to fix the this context. 我没有测试此代码,但您可以理解,也许您必须在某个地方添加$ .proxy()来修复此上下文。

testing if the image is displayed should be easier this way: https://www.customd.com/articles/13/checking-if-an-element-is-visible-on-screen-using-jquery 测试图像是否显示应该通过以下方式更轻松: https : //www.customd.com/articles/13/checking-if-an-element-is-visible-on-screen-using-jquery

I ended that way: 我以这种方式结束了:

setInterval(function(){

                $( ".image" ).each(function( index ) {

                    var img = new Image();
                    img.src = $(this).attr('src');

                    if(img.complete){

                    base_url = $(this).attr('src').split('?rand=')[0];
                    address = base_url + '?rand=' + Math.random();
                    $(this).attr("src", address);

                    }

                });

            },500);

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

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