2014-07-23 197 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'的值! 編輯答案以顯示相同的結果,因爲代碼在意見中看起來無意和混亂。 –