/    Sign up×
Community /Pin to ProfileBookmark

Unable to load image sometime.

I am loading images using javascript. All images are in sequence number. Images sometimes load some times do not.
Below are the functions.
[I][COLOR=”#0000CD”]function onLoad(){
window.resizeTo( 1100,600 )
var ans = [‘a’,’b’,’c’,’d’,’e’];
var fsObj = new Image();

for(var a in ans){
fsObj.src = ‘/images/testquestion/’ + questionNumber + ‘_’ + ans[a] + ‘.jpg’;
if (fsObj.width > 100) {
document.getElementById(‘question’).src = fsObj.src;
realAns[questionNumber]= ans[a];
break;
}
}

}

function loadNextQuestion(){

questionNumber = questionNumber + 1; //first already loaded.
var ans = [‘a’,’b’,’c’,’d’,’e’];
var fsObj = new Image();
for(var a in ans){
fsObj.src = ‘/images/testquestion/’ + questionNumber + ‘_’ + ans[a] + ‘.jpg’;
alert(fsObj.width);
if (fsObj.width >= 100) {
document.getElementById(‘question’).src = fsObj.src;
realAns[questionNumber]= ans[a];
break;
}
}

}[/COLOR][/I]

If I check fsObj.width it shows wrong for the images it does not load. Also if I add some alert before if condition it will work for few images that was not working earlier. I tried flushing Image() object but did not work. Seems something tricky and strange too.?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsSep 23.2013 — the image takes time to load

[CODE]
function onLoad(){
window.resizeTo( 1100,600 )
var ans = ['a','b','c','d','e'];
var fsObj = new Image();
for(var a in ans){
fsObj.src = '/images/testquestion/' + questionNumber + '_' + ans[a] + '.jpg';
}
LoadImage(fsObj); [COLOR="#FF0000"]// only the last scr will be loaded[/COLOR]
}

function LoadImage(img){
clearTimeout(LoadImage.to);
if (img.width < 100) {
LoadImage.to=setTimeout(function(){ LoadImage(img); },100);
}
else { document.getElementById('question').src = img.src;
realAns[questionNumber]= ans[a];
}
}


function loadNextQuestion(){

questionNumber = questionNumber + 1; //first already loaded.
var ans = ['a','b','c','d','e'];
var fsObj = new Image();
for(var a in ans){
fsObj.src = '/images/testquestion/' + questionNumber + '_' + ans[a] + '.jpg';
}
LoadImage(fsObj); // only the last scr will be loaded
}
[/CODE]
Copy linkTweet thisAlerts:
@krjeevauthorSep 23.2013 — Hi vwphillips,

I want to load whichever file exist for ans[a] and there is only 1 for every question number. I do not simple want to load only last ans[a] that is always <questionNumber>_e.
×

Success!

Help @krjeev spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...