且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在继续脚本之前等待图像完全加载的JavaScript

更新时间:2023-10-26 17:36:59

也许这会有所帮助:

currentimage.onload = function(e){
    // code, run after image load
}

如果需要等待图像加载,以下代码将加载下一个图像(currentIndex是你的img变量):

If it is necessary to wait for the image to load, the following code will load the next image (currentIndex is your "img" variable):

var loadImages = function(imageURLarray, currentIndex){
    if (imageURLarray.length == 0 || imageURLarray.length == currentIndex) return false;
    if (typeof currentIndex == 'undefined'){
       currentIndex = 0;
    }
    // your top code
    currentimage.onload = function(e){
        // code, run after image load
        loadImages(imageURLArray, currentIndex++);
    }
}

而不是for循环,例如使用这个函数:

Instead of a "for" loop, use for example this function:

loadImages(imageURLarray);