I have the following code with a setInterval() method. However there is one function in between i.e audio.play(). Whenever setInterval is called the whole function is called again and so the audio plays again and again. Is there any way of run the setInterval() method but have an exception for the audio.play part? Here is the code
setInterval(async () => {
//api stuff
const detections = await faceapi
.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions());
const resizedDetections = faceapi.resizeResults(detections, displaySize);
const happy =resizedDetections[0].expressions.happy;
//The Issue starts from here
if(happy>0.9)
{
audio.src="audios/happy_audio.wav";
audio.play();
}
faceapi.draw.drawDetections(canvas, resizedDetections);
faceapi.draw.drawFaceExpressions(canvas, resizedDetections);
},1000)
})
If you want it not to play while it is playing you could check and see if the audio is currently playing by checking the
paused
property in the conditional:if you just want it to fire once use a flag:
Note I'm not a big fan of variable flags that are globally scoped. In the above case I would recommend a closure, or binding your asynchronous function to an Object that houses the flag, but as an example this should be sufficient :)