Results 1 to 4 of 4

Thread: question about function for preloading images

  1. #1
    Join Date
    Nov 2007
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default question about function for preloading images

    hi,

    would this function work for preloading images?

    Code:
    function Load(){
      var photoNames = ["over0","over1","over2","over3","over4","over5","over6","over7","over8","over9","over10",
    "over11","over12","over13","over14","over15","over16","over2","over17","over18","over19","over20","over21",
    "over22","over23"];
    var photoObjects = [];
    for (var i = 0; i < photoNames.length; i++)
      {
        photoObjects[i] = new Image();
        photoObjects[i].src = 'images/collection/' + photoNames[i] + '.jpg';
      }
    }
    then i would put it as a onload event in the body tag(onload='Load()'), is the function correct?

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    The code mentioned seems to be correct and should work if the image path and names are correct (make sure you have those images in the mentioned path).

  3. #3
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Quote Originally Posted by tetrix View Post
    hi,

    would this function work for preloading images?

    then i would put it as a onload event in the body tag(onload='Load()'), is the function correct?

    Code:
    var photoObjects = [];
    }
    No because you are storing the images in a volatile array that is destroyed as soon as the function terminates, long before the images have time to download. You can use the this keyword to make the array persistent:
    Code:
    function Load(){
      var photoNames = ["over0","over1","over2","over3","over4","over5","over6","over7","over8","over9","over10",
    "over11","over12","over13","over14","over15","over16","over2","over17","over18","over19","over20","over21",
    "over22","over23"];
    
    this.photoObjects = [];
    
    for (var i = 0; i < photoNames.length; i++)
      {
        this.photoObjects[i] = new Image();
        this.photoObjects[i].src = 'images/collection/' + photoNames[i] + '.jpg';
      }
    }

  4. The Following User Says Thank You to clueful For This Useful Post:

    tetrix (09-02-2008)

  5. #4
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Agreed. This would preload images. As for the actual display of image speed depends on your internet service.

    -magicyte

Bookmarks

Posting Permissions

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