1

I have an array of numerical values and, using jQuery, would like to preload images based on that array. For example, if the array is ["1", "5", "8", "23"], I'd like to preload the following images:

/images/1.jpg
/images/5.jpg
/images/8.jpg
/images/23.jpg

What's the best way to accomplish this?

1 Answer 1

2

Source

var cache = [];
var imgArray=  new Array("1", "2", "3");
var prefix= "/images/";
var suffix = ".jpg";

function Preload(arrayName){
    var args_len = arrayName.length;
    for (var i = args_len; i--;) 
    {
        var cacheImage = document.createElement('img');
        cacheImage.src = prefix + arrayName[i] + suffix;
        cache.push(cacheImage);
    }
}

//usage
Preload(imgArray); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.