-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from ValeryYafremau/JS-337
JS-337: [Configurable.JS] Preload all needed images
- Loading branch information
Showing
2 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
define([], function () { | ||
|
||
/** | ||
* Loads images into browser's cash. | ||
* @param {Array <String>} array - List of sources of images. | ||
*/ | ||
var preloadImages = function (array) { | ||
if (!preloadImages.list) { | ||
preloadImages.list = []; | ||
} | ||
var list = preloadImages.list; | ||
|
||
for (var i = 0; i < array.length; i++) { | ||
var img = new Image(); | ||
|
||
img.onload = function() { | ||
var index = list.indexOf(this); | ||
|
||
if (index !== -1) { | ||
list.splice(index, 1); | ||
} | ||
} | ||
|
||
list.push(img); | ||
img.src = array[i]; | ||
} | ||
}; | ||
|
||
return preloadImages; | ||
|
||
}); |