Hii @kartik,
You have to do three thigs:
-
You need to have a list of image URLs as object or array, for example:
var sources = { lion: '/assets/lion.png', monkey: '/assets/monkey.png' };
-
Define the Function definition, where it receives list of image URLs and a callback function in its arguments list, so when it finishes loading image you can start excution on your web page:
function loadImages(sources, callback) {
var images = {};
var loadedImages = 0;
var numImages = 0;
for (var src in sources) {
numImages++;
}
for (var src in sources) {
images[src] = new Image();
images[src].onload = function () {
if (++loadedImages >= numImages) {
callback(images);
}
};
images[src].src = sources[src];
}
}
3.Lastly, you need to call the function. You can call it for example from jQuery's Document Ready
$(document).ready(function (){ loadImages(sources, buildStage); });
Hope it helps!!