简体   繁体   English

如何正确评估if语句?

[英]How can I make my if statement evaluate correctly?

I'm creating a stopwatch that stops after hearing a clap. 我正在创造一个秒表,在听到拍手后停止。 My first function creates a flag variable and calls another. 我的第一个函数创建一个flag变量并调用另一个。 When a loud noise is heard, the flag changes from false to true and should return true to the caller function. 当听到嘈杂的声音时, flagfalse变为true并且应该返回true给调用者函数。 A conditional if statement should stop the stopwatch. 条件if语句应该停止秒表。 However, my stopwatch does not stop after returning true. 但是,我的秒表在返回true后不会停止。

function callStopwatch() {
    var watch = new Stopwatch(timer);
    watch.start();
    var flag = false;
    draw(flag);
    if(flag == true){
        watch.stop();
    }
}

function draw() {
    var vol = mic.getLevel();
    if(vol > 0.2){
        console.log("true"); //created to check whether sound detection is working
        flag = true;
        return flag;
    }
}

console.log returns true to the console when a clap is heard. 当听到一个鼓掌时, console.log会向控制台返回true Therefore, the flag should become true and return as such and evaluate properly in the caller function's if statement. 因此,该标志应该变为true并返回,并在调用函数的if语句中正确评估。 What could I be doing incorrectly? 我可能做错了什么? My guess would be not understanding function scope properly. 我的猜测是不能正确理解功能范围。

All you need to do is to update your flag variable in callStopwatch() after calling function draw. 您需要做的就是在调用函数绘制后更新callStopwatch()中的标志变量。

 function callStopwatch() {
 var watch = new Stopwatch(timer);
 watch.start();
 var flag = false;
 flag = draw(flag); // update flag here
 if(flag == true){
    watch.stop();
 }
 }

Since javascript is async , it won't wait for draw() to return correct value, it will execute your code async , you can try something like this which mentioned under callback . 由于javascript是async ,它不会等待draw()返回正确的值,它会执行你的代码async ,你可以尝试像回调中提到的这样的东西。

draw(flag, function(_flag) {
    if (_flag == true) {
      //to stop watch
      console.log("its true");
    } else {
      console.log("it's false")
    }
  });

Furthermore, your draw() should look something like this. 此外,你的draw()看起来应该是这样的。

function draw(flag, callback) {
  var vol = 0.1; // approx value try changing it to 0.3 will return true based on condition
  if (vol > 0.2) {
    flag = true;
  }
  callback(flag);
}

You are referring different variables in two functions. 您在两个函数中引用了不同的变量。

You are referring local variable flag in callStopwatch() function. 您在callStopwatch()函数中引用局部变量flag

var flag = false;

now you call draw() function and you are referring global flag here 现在你调用draw()函数,你在这里引用全局flag

flag = true;

You set flag to true but that's different variable and won't affect the flag variable in callStopwatch() function 您将flag设置为true,但这是不同的变量,不会影响callStopwatch()函数中的flag变量

change the code to: 将代码更改为:

function callStopwatch() {
  var watch = new Stopwatch(timer);
  watch.start();
  var flag = draw(flag);

  if(flag == true){
    watch.stop();
  }
}

function draw() {
  var vol = mic.getLevel();
  if(vol > 0.2){
    console.log("true"); //created to check whether sound detection is working
    return true;
  }
  return false;
}

First issue is that you are not returning flag value from draw() . 第一个问题是你没有从draw()返回标志值。 You need to remember that flag property has local scope, so inside of draw() function you have a new variable. 您需要记住flag属性具有局部范围,因此在draw()函数内部有一个新变量。 That is why changing flag property inside draw() function does not affect flag property in callStopwatch 这就是为什么在draw()函数中更改flag属性不会影响callStopwatch flag属性

function callStopwatch() {
    var watch = new Stopwatch(timer);
    watch.start();
    if(draw()){
        watch.stop();
    }
}

function draw() {
    let flag = false;
    var vol = mic.getLevel();
    if(vol > 0.2){
        console.log("true"); //created to check whether sound detection is working
        flag = true;
    }
    return flag;
}

Second approach would be to pass a callback to the draw function. 第二种方法是将回调传递给draw函数。

function callStopwatch() {
    var watch = new Stopwatch(timer);
    watch.start();
    draw(function() {watch.stop();});
}

function draw(onCondition) {
    var vol = mic.getLevel();
    if(vol > 0.2){
        console.log("true"); //created to check whether sound detection is working
        onCondition()
    }
}

Now you don't need the flag property at all. 现在你根本不需要flag属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM