我希望我的javascript代码应该等到图像加载
代码:
function imagess(url ,callback)
{
var img = new Image();
img.onError = function() {
alert('Cannot load image: "'+url+'"');
};
img.crossOrigin = '';
img.onload = function() {
callback(img);
};
img.src = url;
}
while(img.isonload){
do nothing no code should execute}
以上代码将在chrome上运行,使用while循环是一种好习惯吗?
答案 0 :(得分:0)
img.onload = function() {
img.onload = true;
callback(img);
};
function callback(img){
while(img.onload)
{
// what do you want
// complete your work and img.onload set false for stop execution of loop
}
}
当卸载函数执行时,回调就是调用它做你的东西