我遇到了腳本問題。預加載圖片不起作用
我想加載數組中的所有圖像,然後檢查它們是否全部加載,然後再繼續。但它不起作用我也沒有得到任何錯誤,所以我不知道什麼是錯的。
這是我有:
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element){
window.setTimeout(callback, 200/100);
};
})();
function img_loader(){
for(var i in Images){
Images[i]['img_src'].onload = function() {
countImages ++;
}
}
}
function img_load_checker(){
if(countImages == Images.length){
return true;
} else {
requestAnimFrame(img_load_checker); // keep checking
}
}
window.countImages = 0;
img_loader();
if(img_load_checker()){
//this never gets executed - reason is unknown
//continue with rest of the script
}
這是結構console.log(Images);
[1: Object, 2: Object]
1: Object
img_src: <img>
2: Object
img_src: <img>
任何一個能看到錯誤?
當你說 「它不工作」 你是什麼意思明確? –
你試過調試你的代碼嗎?主持人說了什麼? JSLint說什麼? – elclanrs
'//繼續使用腳本的其餘部分'腳本的這部分內容不會執行。 – Sir