2016-10-31 58 views
-1

我有問題,該行的Javascript全局變量 - 控制檯日誌未定義

console.log("xxx" + stanica1); 

結果是xxxundefined,但我需要返回值stanica1Prosjek/24;

function podatciPrethodniDan(handleData) { 
    var parametar = $("#parametar1").val(); 

    $.ajax({ 
    type: "POST", 
    url: "php/getPreviousDayData.php", 
    dataType: "json", 
    data: { 
     parametar: parametar 
    }, 
    success: function(data) { 
     handleData(data); 
     } //end of success 
    }); //end of ajax 
} 

function style(feature) { 
    var stanica1; 


    stanica1 = podatciPrethodniDan(function(output) { 
    //console.log(output); 

    var stanica1Prosjek = 0; 
    var stanica2Prosjek = 0; 
    var stanica3Prosjek = 0; 
    var stanica4Prosjek = 0; 

    //console.log(output.length); 
    for (i = 0; i < output.length; i++) { 
     //console.log("petlja " + i); 
     if (i < 24) { 
     stanica1Prosjek = stanica1Prosjek + parseFloat(output[i].par); 
     //console.log(stanica1Prosjek + " " + i); 
     } 
    } 
    console.log("in" + stanica1Prosjek); 
    return stanica1Prosjek/24; 

    }); 

    console.log("xxx" + stanica1); 

} 
+2

看起來不像你曾經調用'stanical',你只是定義它 –

+0

stanica1 = podatciPrethodniDan(函數(輸出)... –

+0

@tcooc沒有重複,OP?正在使用回調 –

回答

-1

定義你var函數。 例子:

var demo = 12; 
function ok() { 
    demo += 3; 
} 


function ok2() { 
    demo+= 5; 
} 

console.log(demo); // return 12 
ok(); 
console.log(demo); // return 15 
ok2(): 
console.log(demo); // return 20 
+3

這不是有問題的問題 –