admin管理员组文章数量:1287523
I have a request calling up a bunch of images like so:
<a href='www.domain1'><img src='../image/img1.png' onerror='imgError(this);'/></a>
<a href='www.domain2'><img src='../image/img2.png' onerror='imgError(this);'/></a>
The problem is when the call is made some of the images (~20%) are not ready yet. They need another second.
So in js or jquery what I would like to do is on error get the images that failed, wait 1 second, then try to load those failed images again. If they fail on the 2nd try -- oh well, Im okay with that. But Im not doing this correctly... Should I not be calling a timeout inside of another method in js?
function imgError(image) {
image.onerror = "";
image.src = image;
setTimeout(function () {
return true;
}, 1000);
}
I have a request calling up a bunch of images like so:
<a href='www.domain1.'><img src='../image/img1.png' onerror='imgError(this);'/></a>
<a href='www.domain2.'><img src='../image/img2.png' onerror='imgError(this);'/></a>
The problem is when the call is made some of the images (~20%) are not ready yet. They need another second.
So in js or jquery what I would like to do is on error get the images that failed, wait 1 second, then try to load those failed images again. If they fail on the 2nd try -- oh well, Im okay with that. But Im not doing this correctly... Should I not be calling a timeout inside of another method in js?
function imgError(image) {
image.onerror = "";
image.src = image;
setTimeout(function () {
return true;
}, 1000);
}
Share
Improve this question
edited Oct 30, 2013 at 4:56
Samuel O'Malley
3,5511 gold badge25 silver badges42 bronze badges
asked Oct 30, 2013 at 3:23
ChrisChris
18.9k15 gold badges61 silver badges79 bronze badges
6
-
Why in the world do you want to do this? Besides the issues in your implementation, I suspect that setting
img.src
to what it's already set doesn't do anything. – jfriend00 Commented Oct 30, 2013 at 3:26 - Haha. Well. Im actually using imagemajick in between the initial ajax and loading the images. What is the better practice for something like this? Thank u btw 4 the ment... – Chris Commented Oct 30, 2013 at 3:28
- 1 Check out this answer here: stackoverflow./questions/4285042/can-jquery-ajax-load-image – Samuel O'Malley Commented Oct 30, 2013 at 3:28
- FYI, @SamuelO'Malley, though the OP mentions AJAX, I believe there is no AJAX here, or needing to be here. – Paul Draper Commented Oct 30, 2013 at 4:31
- Thanks @PaulDraper, I wasn't sure if he actually wanted to use AJAX or not. – Samuel O'Malley Commented Oct 30, 2013 at 4:50
1 Answer
Reset to default 12Add a cache breaker.
function imgError(image) {
image.onerror = null;
setTimeout(function (){
image.src += '?' + +new Date;
}, 1000);
}
(This assumes your image URL doesn't already have a query string, per the example. If it does, a little more work is obviously required.)
本文标签: javascriptJSJQuery Retry Img Load After 1 SecondStack Overflow
版权声明:本文标题:javascript - JSJQuery Retry Img Load After 1 Second - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741311327a2371665.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论