akasma74 / Hass-Custom-Alarm

It is a fork of "Yet another take on a home assistant custom alarm" that will exist until its author is back to our Earth
80 stars 31 forks source link

Add optional beeping on device while arming/warning #121

Open TJPoorman opened 3 years ago

akasma74 commented 3 years ago

Thanks for the PR. So basically it defines playSound and invokes it when required (and also adds that option to Settings), is that correct? Could you explain me what this thing does (I know it's some JS trickery but I'm not a web developer and can only guess, which is not the right thing in this situation)?

this.countdownTimerId = setInterval(() => this.playSound(), 1000);

TJPoorman commented 3 years ago

Yes that's correct. setInterval is a JS function that creates a timer to run in the background that will execute the function (the first argument) every X milliseconds (the second argument). The "() =>" part is an ES6 arrow function which allows the binding of "this" which makes it a cleaner shorter syntax. setInterval optionally returns a unique ID for the timer that is created, that is what is being stored in the "this.countdownTimerId" variable. That is used when the timer is finished counting down that it removes it so it does not continue to beep.