对于循环图像库

时间:2014-10-28 20:45:35

标签: javascript jquery html json for-loop

我正在尝试从JSON Feed创建图库。以下是我在下面使用的代码:

for (var i = 0; i < json.images.length; i++){
        var image_thumbnail_url = json.images[i].image_thumbnail_url;
        var image_alt_text = json.images[i].image_alt_text;
        var image_category = json.images[i].image_category;
        var image_url = json.images[i].image_url;
        var dealer_company_name = json.images[i].dealer[0].dealer_company_name;
        var dealer_email_address = json.images[i].dealer[0].dealer_email_address;
        var dealer_website_url = json.images[i].dealer[0].dealer_website_url;
        var dealer_phone_number = json.images[i].dealer[0].dealer_phone_number;
        $("#main").append("<img class='thumbnail' src='" + image_thumbnail_url + "' alt='"+ image_alt_text +"' data-image-category='"+ image_category +"' data-image-url='"+ image_url +"' data-dealer-name='"+ dealer_company_name +"' data-dealer-email-address='"+ dealer_email_address +"' data-dealer-website='"+ dealer_website_url +"' data-dealer-phone-number='"+ dealer_phone_number +"'>");
    };

目前页面没有逐个输出图像 - &gt;它将它们全部装在一起。这反过来导致非常长的页面加载时间。有没有办法让我可以逐个加载图像,这样页面加载时间就会很短。

提前致谢!

修改

我刚刚实现了这个:

for (var i = 0; i < json.images.length; i++){
        appendImage(json.images[i].image_thumbnail_url, json.images[i].image_alt_text, json.images[i].image_category, json.images[i].image_url, json.images[i].dealer[0].dealer_company_name, json.images[i].dealer[0].dealer_email_address, json.images[i].dealer[0].dealer_website_url, json.images[i].dealer[0].dealer_phone_number)
    };

    function appendImage(image_thumbnail_url, image_alt_text, image_category, image_url, dealer_company_name, dealer_email_address, dealer_website_url, dealer_phone_number) {
        setTimeout(function() {
             $("#gui_sub").append("<img class='gui_thumbnail' src='" + image_thumbnail_url + "' alt='"+ image_alt_text +"' data-image-category='"+ image_category +"' data-image-url='"+ image_url +"' data-dealer-name='"+ dealer_company_name +"' data-dealer-email-address='"+ dealer_email_address +"' data-dealer-website='"+ dealer_website_url +"' data-dealer-phone-number='"+ dealer_phone_number +"'>");
        },300 * i); 
    }

这似乎完全可以让图像逐个加载 - &gt;但页面加载时间仍然相同:(

1 个答案:

答案 0 :(得分:2)

如果您不想在应用程序中引入任意数量的延迟,可以在上一次加载后立即加载下一个:

(function loadNextImage(i) {
    if (json && json.images && json.images[i]) {
        var img = new Image(),
            image = json.images[i];
        img.onload = function () {
            $("<img/>", {
                src: image.image_thumbnail_url, // will be cached
                alt: image.image_alt_text,
                'data-image-category': image.image_category,
                'data-dealer-name': image.dealer[0].dealer_company_name,
                'data-dealer-email-address': image.dealer[0].dealer_email_address,
                'data-dealer-website': image.dealer[0].dealer_website_url,
                'data-dealer-phone-number': image.dealer[0].dealer_phone_number
            }).appendTo("#main");
            loadNextImage(++i);
        };
        img.src = image.image_thumbnail_url;
    }
})(0);