Javascript jquery同步执行

Javascript jquery同步执行,javascript,jquery,synchronous,Javascript,Jquery,Synchronous,我正在尝试获取远程图像的资源 function getMaxRes(url) { var imgHeight=0; $("<img/>").attr("src", url).load(function(){ s = {w:this.width, h:this.height}; imgHeight = this.height ; console.log(imgHeight); // printed corre

我正在尝试获取远程图像的资源

function getMaxRes(url)
{
    var imgHeight=0;
     $("<img/>").attr("src", url).load(function(){
         s = {w:this.width, h:this.height};
         imgHeight =  this.height ;   
        console.log(imgHeight); // printed correctly

      }); 
     console.log(imgHeight); //prints 0

}
getMaxRes(< url to the image > )
函数getMaxRes(url) { var imgHeight=0; $(" )
我尝试过使用全局变量。如何使函数返回图像高度。

使用回调。不要使其同步。首先绑定到加载事件,然后设置src。但无论如何都必须使用回调。还要注意,在jQuery 1.9+中,
.load
不绑定加载事件。