-1

I have problem with this line

 console.log("xxx" + stanica1);

Result is xxxundefined but i need return value 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);

}
epascarello
  • 204,599
  • 20
  • 195
  • 236
marko marinovic
  • 103
  • 1
  • 9
  • 2
    Doesn't look like you're ever calling `stanical`, you're just defining it – Sterling Archer Oct 31 '16 at 17:41
  • stanica1 = podatciPrethodniDan(function(output) ... ? – marko marinovic Oct 31 '16 at 17:42
  • @tcooc not a duplicate, OP is properly using a callback. – Sterling Archer Oct 31 '16 at 17:43
  • 2
    @SterlingArcher From looking at the given code, the OP is trying to return `stanica1Prosjek/24` which is run asynchronously, and save the value to `stanica1` (which is `console.log`ged synchronously). This is not possible without adding an *additional* callback or reworking the code. – tcooc Oct 31 '16 at 17:45
  • @SterlingArcher well, OP is using a callback but the `console.log` is still immediately after calling the function with the callback in it, so it's still going to be populated when the callback executes which is _after_ that point in time. – VLAZ Oct 31 '16 at 17:51
  • So the OP needs a callback for the callback. ;) – epascarello Oct 31 '16 at 17:53
  • @epascarello Yo dawg... – VLAZ Oct 31 '16 at 17:54

1 Answers1

-1

Define you var out of functions. Example:

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
Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73