Avoid playing sound more than every 2s

This commit is contained in:
binwiederhier
2026-03-08 10:55:59 -04:00
parent fcf95dc9b8
commit 109271a930
2 changed files with 16 additions and 4 deletions
+9 -1
View File
@@ -8,6 +8,8 @@ import routes from "../components/routes";
* support this; most importantly, all iOS browsers do not support window.Notification.
*/
class Notifier {
lastSoundPlayedAt = 0;
async notify(subscription, notification) {
if (!this.supported()) {
return;
@@ -49,11 +51,17 @@ class Notifier {
}
async playSound() {
// Play sound
// Play sound, but not more than once every 2 seconds
const now = Date.now();
if (now - this.lastSoundPlayedAt < 2000) {
console.log(`[Notifier] Not playing notification sound, since it was last played <2s ago`, this.lastSoundPlayedAt);
return;
}
const sound = await prefs.sound();
if (sound && sound !== "none") {
try {
await playSound(sound);
this.lastSoundPlayedAt = Date.now();
} catch (e) {
console.log(`[Notifier] Error playing audio`, e);
}