0

I would like to preload different folders with images inside before to show my index.html. If I can show i div with loader and text during the process it will be perfect

I found this part of code from here: http://www.javascriptkit.com/javatutors/preloadimagesplus.shtml

and update it with my different links, is it ok and good start ?

function preloadimages(arr) {
  var newimages = [],
    loadedimages = 0
  var postaction = function() {}
  var arr = (typeof arr != "object") ? [arr] : arr

  function imageloadpost() {
    loadedimages++
    if (loadedimages == arr.length) {
      postaction(newimages) //call postaction and pass in newimages array as parameter
    }
  }
  for (var i = 0; i < arr.length; i++) {
    newimages[i] = new Image()
    newimages[i].src = arr[i]
    newimages[i].onload = function() {
      imageloadpost()
    }
    newimages[i].onerror = function() {
      imageloadpost()
    }
  }
  return { //return blank object with done() method
    done: function(f) {
      postaction = f || postaction //remember user defined callback functions to be called when images load
    }
  }
}

preloadimages([
  'http://www.domain.com/wp-content/uploads/book0/',
  'http://www.domain.com/wp-content/uploads/book1/',
  'http://www.domain.com/wp-content/uploads/book2/',
  'http://www.domain.com/wp-content/uploads/book3/',
  'http://www.domain.com/wp-content/uploads/book4/'
]).done(function(images) {})
clodo0683
  • 89
  • 2
  • 3
  • 10
  • Anything you could take from here? http://stackoverflow.com/a/11072778/383904 – Roko C. Buljan Oct 26 '16 at 08:32
  • 1
    I'm not sure what you are asking. What happens, what did you expect to happen, and what have you tried? –  Oct 26 '16 at 08:37
  • this loader isn't load images, we can use it only to win time ? – clodo0683 Oct 26 '16 at 08:43
  • i tried anything at this time i would like to know how can i preload my images who are on different folders before to let visitors navigate on my website @AllDani – clodo0683 Oct 26 '16 at 08:45
  • Please see [mcve] –  Oct 26 '16 at 08:46
  • There is for sure one major mistake in the code: `newimages[i].src = arr[i]` has to be AFTER the onload and onerror. you need to put the above code in the head of the page or on a previous page. You can set the body tag's display to hidden and to visible in the callback while showing a "please wait" – mplungjan Oct 26 '16 at 09:25

0 Answers0