I'm creating a stopwatch that stops after hearing a clap. My first function creates a flag
variable and calls another. When a loud noise is heard, the flag
changes from false
to true
and should return true
to the caller function. A conditional if
statement should stop the stopwatch. However, my stopwatch does not stop after returning 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. Therefore, the flag should become true
and return as such and evaluate properly in the caller function's if
statement. What could I be doing incorrectly? My guess would be not understanding function scope properly.
Async
, it won't wait fordraw()
to return correct flag, you can try callback instead! – Dhaval Marthak May 6 '19 at 4:58