Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

Stop if else loop when the condition is met using React Native

I'm trying to fetch data using Nutritionix API and do a looping until the condition is met inside the function. But the loop keeps on going even after the condition is met already. What I am trying to do is add the value for running time by 10 minutes until the condition is met which is the calories differences.

I'm pretty sure I implemented the function in a wrong way. I hope you can enlighten me at what point that I'm breaking the function.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...