Answer the question
In order to leave comments, you need to log in
Tracking the progress of image caching?
Hello!
There is the following javascript code:
function preloadImages(array) {
if (!preloadImages.list) {
preloadImages.list = [];
}
for (var i = 0; i < array.length; i++) {
var img = new Image();
img.src = path+array[i];
preloadImages.list.push(img);
}
}
Answer the question
In order to leave comments, you need to log in
Not a very authoritative source, but here is image.complete The
Image object has a complete flag, if the image is already loaded and will be taken from the cache, the flag is set to true.
var img = new Image();
img.onload = function(){
count++;
}
if(image.complete){
count++;
}
if(count == imageArray.length) doSomething();
During the loading of an image, there are three options for subsequent events with it:
1) The image will be loaded (the load event will
fire
) abort event)
You can use the following code:
var img = new Image();
img.onload = function(){
console.log('img is loaded!');
}
img.onerror = function(){
console.log('there is some error, when trying to load img');
}
img.onabort = function(){
console.log('img loading is aborted');
}
img.src = 'https://www.google.com/images/srpr/logo4w.png';
That is, when any of the above events fires, you increment some variable (for example, “processed”), and update the percentage of images “processed” by the browser like this:
updateLoderView( processed/ preloadImages.list.length);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question