www.webdeveloper.com
Results 1 to 3 of 3

Thread: Unable to load image sometime.

  1. #1
    Join Date
    Sep 2013
    Posts
    2

    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.
    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;
    }
    }
    }


    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.

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,645
    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); // only the last scr will be loaded
    }
    
    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
    }
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Sep 2013
    Posts
    2
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles