Javascript全局变量 - 控制台日志未定义

时间:2016-10-31 17:39:56

标签: javascript global

我有这条线的问题

 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);

}

1 个答案:

答案 0 :(得分:-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