Since JavaScript has been doing for only a month, I have not really learned the details yet. Need clarification on how the code works. Namely - what happens in each line of code:

function preloadImages() { if (typeof arguments[arguments.length - 1] == 'function') { var callback = arguments[arguments.length - 1]; } else { var callback = false; } if (typeof arguments[0] == 'object') { var images = arguments[0]; var n = images.length; } else { var images = arguments; var n = images.length - 1; } var not_loaded = n; for (var i = 0; i < n; i++) { jQuery(new Image()).attr('src', images[i]).load(function() { if (--not_loaded < 1 && typeof callback == 'function') { callback(); } }); } } 

Closed due to the fact that the issue is too general for the participants Regent , Sergey Snegirev , aleksandr barakin , ixSci , dizballanze 7 Jul '15 at 8:28 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    In each line? This is then a question for teachers who teach for money. - Regent
  • You can not in each) - Valeriy1996
  • Which line is the problem? - Regent

2 answers 2

Comments on the code:

 function preloadImages() { // //Смотрим была ли передана функция callback последним параметром //preloadImages(function(){}) // if (typeof arguments[arguments.length - 1] == 'function') { var callback = arguments[arguments.length - 1]; } else { var callback = false; } // //Проверяем как были переданы пути картинок //массивом: preloadImages(["a.jpg", "b.jpg"]) //или через запятую: preloadImages("a.jpg", "b.jpg") // if (typeof arguments[0] == 'object') { var images = arguments[0]; var n = images.length; } else { var images = arguments; var n = images.length - 1; } var not_loaded = n; // //Создаем новый объект класса Image //Устанавливаем поле src - путь к картинке //Подписываемся на событие load //Если была задана callback-функция, вызываем ее // for (var i = 0; i < n; i++) { jQuery(new Image()).attr('src', images[i]).load(function() { if (--not_loaded < 1 && typeof callback == 'function') { callback(); } }); } } 

You can make it easier and more beautiful:

 function preload(sources) { var images = []; for (i = 0, length = sources.length; i < length; ++i) { images[i] = new Image(); images[i].src = sources[i]; } } 

or

 $.fn.preload = function() { this.each(function(){ $('<img/>')[0].src = this; }); } 

Taken from here and here .

    This is an imitation overload function:

     preloadImages([url1, url2, ...]); preloadImages([url1, url2, ...], callback); preloadImages(url1, url2, ..., callback); // preloadImages(url1, url2, ...); // Потеряли из-за бага 

    The function itself loads all images and calls a callback, if it has been provided.

    • If the last parameter is a function, then this is a callback . Otherwise there is no kollbek.
    • If the first parameter is an object, then we consider that this image is an array with images.
    • If the first parameter is not an object, then we expect a call in the preloadImages(url1, url2, ..., callback) style preloadImages(url1, url2, ..., callback) and retrieve images as all arguments except the last one. Not exactly, but this behavior is emulated by using n instead of length. This is a bug! It is impossible to transfer addresses one by one without transferring a callback - the last address will be lost.
    • Load a piece of pictures. When loading, decrease not_loaded - the number of pictures that not_loaded be downloaded.
    • If all the pictures are loaded and there is a callback, then we call it.

    PS: The function is not well written, the constant declaration of the same variables is annoying. Failure to use the conditional operator - too. Well, the typeof check is at the end, although there is guaranteed to be either a function or false.