使用 React Native 当条件满足时停止 if else 循环

时间:2021-01-06 16:58:58

标签: react-native loops if-statement axios

我正在尝试使用 Nutritionix API 获取数据并进行循环,直到在函数内部满足条件。但是即使在条件已经满足之后,循环也会继续进行。我想要做的是将运行时间的值增加 10 分钟,直到满足条件,即卡路里差异。

我很确定我以错误的方式实现了该功能。我希望你能在我破坏功能的哪一点启发我。

let running = 10;
let walking = 10;
var postDataCalories = {
  query": `${running} minutes run and ${walking} minutes walking`,
  "gender":"male",
  "weight_kg":63.5,
  "height_cm":167.64,
  "age":30
};

function axiosPostExercise(){
          axios.post('https://trackapi.nutritionix.com/v2/natural/exercise', postDataCalories, axiosConfig2)
          .then((res3) => {
            console.log("RESPONSE RECEIVED: ", res3);
            let caloriesFood = res2.data.foods[0].nf_calories;
            let caloriesExercise = res3.data.exercises[0].nf_calories;
            let caloriesDifferences = caloriesFood - caloriesExercise;
            if (caloriesDifferences < caloriesDifferences - 10 || caloriesDifferences > caloriesDifferences + 10){
              console.log(caloriesDifferences);
              $("#calories_burned").html("" +
          
              "<br><div class='rounded-box burn-calories ng-scope'>" +
              "<div class='box-title ng-binding'> How long would it take to burn off " + caloriesFood + " KCal?" +
              "</div>" +
              "<div class='box-content'>" +
              "  <table class='table m-b-none'>" +
              "    <tr>" +
              "      <td>" + res3.data.exercises[0].name + "</td>" +
              "      <td class='ng-binding'>" + res3.data.exercises[0].duration_min + " minutes</td>" +
              "    </tr>" +
              "   <tr>" +
              "     <td>" + res3.data.exercises[1].name + "</td>" +
              "     <td class='ng-binding'>" + res3.data.exercises[1].duration_min + " minutes</td>" +
              "   </tr>" +
              "   </tbody>" +
              " </table>" +
              " <div>" +
              " <small> Values estimated based on person weighing 140 lbs." +
              "   </small>" +
              " </div>" +
              "</div>" +
              "</div>");
            } else {
              running+10;
              axiosPostExercise();
            }
          })
        }

        axiosPostExercise();

0 个答案:

没有答案