当尝试加载不存在的外部图像时,使用超时停止加载图像

发布于 2024-08-14 07:49:22 字数 290 浏览 3 评论 0原文

我有一个动态加载图像的 JSP 页面。现在,我在此页面上还有一些“输入”元素,例如使用 javascript 模块创建的复选框和选择菜单(为输入元素创建不同的样式)。

我的问题是,页面尝试加载的图像之一在它尝试打开的外部链接上不存在。服务器仅在 15 秒后响应说该图像不存在。这意味着我必须等待 15 秒才能加载“输入”元素,因为只有在页面上的所有图像完全加载后才会激活 javascript 模块。

有没有办法让我设置加载图像的超时时间,例如,在尝试加载图像 5 秒后,它会停止请求?

谢谢,

迈克尔

I have got a JSP page which loads images dynamically. Now, I also have some "input" elements on this page such as a checkbox and select menus which are being created using a javascript module (creating a different styling for the input elements).

My issue is, one of the images the page is trying to load does not exist on the external link it is trying to open. The server only responds after 15 seconds to say that the image does not exist. This means that I have to wait 15 seconds for my "input" elements to load, since the activation of the javascript module only happens after all images are fully loaded on my page.

Is there a way for me to set a timeout for loading images, such that for example, after 5 seconds of trying to load an image, it stops the request?

Thanks,

Michael

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

梦里人 2024-08-21 07:49:22

如果您希望脚本在加载/不加载图像之前执行,您应该使用 DOMready 事件或在关闭 body 标记之前触发模块。

关于检查图像是否存在,您可以执行以下操作:

var img = $(new Image()).load(function() {
    // image exists
}).error(function() {
    // image does not exist
}).attr('src','some-image-url.jpg');

这应该可以跨浏览器工作(它使用 onError 事件:https://developer.mozilla.org/En/XUL/Image#a-onerror)

You should use a DOMready event or trigger the module before closing the body tag if you want your scripts to execute before images are loaded/not loaded.

Regarding checking if image exists, you can do something like:

var img = $(new Image()).load(function() {
    // image exists
}).error(function() {
    // image does not exist
}).attr('src','some-image-url.jpg');

This should work cross-browser (it uses the onError event: https://developer.mozilla.org/En/XUL/Image#a-onerror)

浮世清欢 2024-08-21 07:49:22

我认为这是不可能的,因为加载图像的过程在浏览器中根深蒂固,无法直接访问或终止。

可能的解决方法:

5 秒后检查图像是否具有正确的尺寸。如果没有,请删除图像的 DOM 元素。我不确定这是否会停止加载,但我认为应该停止加载。

I think this can't be done as the process of loading images is very deeply entrenched in the browser and can't be accessed or terminated directly.

A possible workaround:

Check after 5 seconds whether the image has proper dimensions yet. If it doesn't, remove the image's DOM element. I'm not sure this will stop loading but I think it should.

一个人练习一个人 2024-08-21 07:49:22

非常感谢您的所有回答,您给了我很好的线索,让我知道我应该做什么。

在创建“input”元素的 JavaScript 模块中,在脚本底部编写了以下行:

window.onload = Custom.init;

我基本上取出了这一行,并将其写入我的 header.jsp 文件中,该文件包含在我的所有页面中,方法如下:

$(文档).ready(function() {

自定义.init();

});

再次感谢您的快速回复,

迈克尔

Thank you very much for all your answers, you have given me great clues as to what I should have actually done.

In the JavaScript module that creates the "input" elements, the following line was written at the bottom of the script:

window.onload = Custom.init;

I basically took this line out, and wrote it instead in my header.jsp file which is included in all of my pages, in the following way:

$(document).ready(function() {

Custom.init();

});

Thanks again for your quick replies,

Michael

心不设防 2024-08-21 07:49:22

您可以简单地在加载图像之前设置超时以及测试加载错误。

function LoadImage(yourImage) {

    var imageTimer = setTimeout(function () {
        //image could not be loaded:
        alert('image load timed out');
    }, 10000); //10 seconds

    $(yourImage).load(function (response, status, xhr) {
        if (status == 'error') {
            //image could not be loaded:
            alert('failed to load image');

        } else {
            //image was loaded:
            clearTimeout(imageTimer);
            //display your image...
        }
    });

}

You could simply set a timeout before loading the image as well as testing for a load error.

function LoadImage(yourImage) {

    var imageTimer = setTimeout(function () {
        //image could not be loaded:
        alert('image load timed out');
    }, 10000); //10 seconds

    $(yourImage).load(function (response, status, xhr) {
        if (status == 'error') {
            //image could not be loaded:
            alert('failed to load image');

        } else {
            //image was loaded:
            clearTimeout(imageTimer);
            //display your image...
        }
    });

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