0

in JS i get my data from php. But when i will get the min or max value it is not working. it is Infinity. What ist wrong with this myTemp1 array?

  let myTime =[];
  let myTemp1 =[];
  let myHumi1 =[];


  $.get("./php/get_temp_humi_last_24.php", function(data){
    var myValue = jQuery.parseJSON(data);
      myValue.forEach(function(row){
      myTime.push(row.time);
      myTemp1.push(parseFloat(row.ch1_Temp));
      myHumi1.push(parseFloat(row.ch1_Humi));
    });
  });


  console.log(Math.min(...myTemp1));
markus_he
  • 17
  • 1
  • `myTemp1` is an empty array as of when you call `Math.min`, see the linked question's answers for why. `Math.min` returns `Infinity` if you don't pass it any arguments. – T.J. Crowder Feb 04 '22 at 11:23
  • but why is this empty, it was filled with the data from $.get – markus_he Feb 04 '22 at 11:42
  • ***"see the linked question's answers for why"*** Ajax is **asynchronous**. You're using the array before the operation completes, so nothing has been pushed into it yet. – T.J. Crowder Feb 04 '22 at 11:44
  • i saw your linked answer, but why are myTemp1 later in my chartjs as data working? – markus_he Feb 04 '22 at 11:47
  • Please read the answers to the linked question carefully. At some point, the array will have values, so if you're seeing values later in your code, it's because that code runs **after** the ajax call has finished and populated the array. When you get off the phone after ordering a pizza, you don't instantly have the pizza, but you do have it *later* after it's delivered. – T.J. Crowder Feb 04 '22 at 11:49
  • i anderstand what you meen and what happens. baut what is the best solution for this problem? – markus_he Feb 04 '22 at 12:12
  • Don't try to use the value until you have the value. Wait for the ajax call to finish. – T.J. Crowder Feb 04 '22 at 12:19

0 Answers0