2014-07-23 149 views
1

我有一个名为isPortrait的JS函数来确定给定照片的方向。问题是,当我打电话时,它总是返回false。但是,alert()完美地工作,并返回照片的分辨率。我该如何解决它?JavaScript函数总是返回false

function isPortrait(src) { 
    var is = false; 
    var img = new Image(); 
    img.src = src; 
    img.onload = function() { 
     alert(img.width+"x"+img.height); 
     if(img.height>img.width) 
     { 
      is = true; 
     } 
    } 
    return is; 
} 

我的解决方案(使用全局变量):

window.isportrait = false; 
function isPortrait(src) { 
    var img = new Image(); 
    img.src = src; 
    img.onload = function() { 
     if(img.height>img.width) 
     { 
      window.isportrait = true; 
     } 
     else 
     { 
      window.isportrait = false; 
     } 
    } 
} 
+0

你的问题是关于JavaScript和我已经回答了。然而,使用Jquery将使你更容易执行你想要的任务。尽可能考虑它 – stackunderflow

回答

1

该函数在异步调用完成之前返回false!

最佳执行我能到达这里JSBIN example

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>JS Bin</title> 
</head> 
<body> 
<h1 id="heading">? </h1> 
</body> 
</html> 
  • 和使用JavasScript代码:

    var is = false; 
    
    function isPortrait(src) { 
    
    var img = new Image(); 
    img.src = src; 
    var here = img.onload = function() { 
    
        alert(img.width+"x"+img.height); 
        if(img.height>img.width) 
        { 
         is = true; 
    
    
        } 
        // it is here at this moment where you can get result of the 
    // awaited async function onload() 
    document.getElementById("heading").innerHTML = "is it Portrait ? " + is; 
    
        }; 
    
    
    
    } 
    
    
        isPortrait("http://blogs.smithsonianmag.com/aroundthemall/files/2009/01/npg_portraits_nicholson_jack_2002-244x300.jpg"); 
    

编辑[使用“setInterval`另一个更好impleentation

 var is = false; 
    var notYetLoaded = true; 


    function isPortrait(src){ 
     tryLoadImage(src); 

    var myWaiting = setInterval(function(){ 
    if(notYetLoaded === false){ 


    // use the "is" boolean here 

    document.getElementById("heading").innerHTML = "is it Portrait ? " + is; 
    clearInterval(myWaiting); 
    } 
    }, 1000); 
    } 


    function tryLoadImage(src) { 

    var img = new Image(); 
    img.src = src; 
    var here = img.onload = function() { 

     alert(img.width+"x"+img.height); 
     if(img.height>img.width) 
     { 
      is = true; 


     } 
     notYetLoaded = false; 
     }; 



    } 


     isPortrait("http://blogs.smithsonianmag.com/aroundthemall/files/2009/01/npg_portraits_nicholson_jack_2002-244x300.jpg"); 

代码在这里:http://jsbin.com/qodefuhe/1/edit

我希望帮助:)

+0

1.这里没有Ajax调用。 2.这将无法工作。它可能适用于您的情况,因为图像缓存在浏览器中,并且在运行脚本时已经加载。 – VisioN

+0

谢谢你的修正,它是异步而不是ajax调用[我编辑它]:P,但你会告诉我为什么它不会工作?我的意思是,当我提供一个'Img.src'时,为什么不加载事件工作? – stackunderflow

+0

顺便说一句,我冲洗浏览器缓存后,它测试它,它的作品! – stackunderflow

4

你的函数总是返回false因为onload事件被称为异步,这基本上意味着该函数将返回is之前onload函数被执行。这就是为什么,使其工作,建议使用callbacks

function do_something() { 
    // do something 
} 

function isPortrait(src) { 
    var img = new Image(); 
    img.src = src; 
    img.onload = function() { 
     if (img.height > img.width) { 
      do_something(); 
     } 
    }; 
} 
+0

现在它返回undefined。 –

+1

@BalázsVarga您*无法*从异步调用中返回值。在你的情况下'onload'不能返回一个值到'isPortrait'函数。 – VisioN

+0

我已经解决了它与设置一个全局变量(window.isportrait),并在函数中设置它的值。然后在超时(我已经在我的背景旋转代码中)检查它。 –

0

这是发生,因为isPortrait()将返回的“是”前值的onload功能的完整执行。

您可以尝试以下内容,在其中声明全局变量'is'并调用另一个函数onload并返回来自新函数的is的值。

var is = false; 
var img = new Image(); 
function isPortrait(src) { 
    img.src = src; 
    img.onload = temp(src); 
} 

function temp(src) { 
    is = test(src); 
} 

function test(src) { 
     alert(img.width+"x"+img.height); 
     if(img.height>img.width) 
     { 
      is = true; 
     } 
     return is; 
} 
+0

要将“is”的值返回到哪里? – VisioN

+0

是的,这是一个从我身边的小姐:) 在这种情况下,我们需要一个更多的功能来接收'is'的值! 编辑答案以显示相同的结果,因为代码在意见中看起来无意和混乱。 –