I'm not good enough at the basic mechanics of web browsers to find out if this will work with svg image tags, but I have had successful caching of images using the new Image ():
//download low quality images var imageArray = [] for (var i = 0; i < 600; i++){ imageArray[i] = new Image(); imageArray[i].src = moviePath + (i + 1) + '.jpg.t'; imageArray[i].onload = function(){ var i = this.src.split(movieName + '/')[1].split(".")[0]; d3.select("#bar" + i).style("stroke", 'rgb(' + colors[i].rgb + ')'); } }
To show the image, I just set the src of the displayed image to the one that was already loaded, and the browser loads it from my cache.
There is another little trick used later in - First display a low quality image and start downloading a high quality image only after a short timeout passes without selecting another image. Then, after downloading the image with high quality, show it only if it is still selected.
I don't know if these are best practices or anything, but it worked quite well .
source share