ballz.js/sound-manager.js
2019-04-10 17:58:02 +02:00

20 lines
465 B
JavaScript

class SoundManager {
constructor(soundSource) {
this.soundSource = soundSource;
this.sound = new Audio(this.soundSource);
this.lastPlayed = 0;
}
play() {
if (Date.now() - this.lastPlayed < 50)
return;
const node = this.sound.cloneNode();
node.play();
setTimeout(() => {
node.remove();
}, this.sound.duration * 1000);
this.lastPlayed = Date.now();
}
}