I'm trying to implement an app that creates a ".message" for each lyric every time the beat ticks. However, it seems like I've hit a wall while trying to come up with an algorithm.
My code:
import EventEmitter from "eventemitter3";
import Beat from "./Beat";
export default class Application extends EventEmitter {
static get events() {
return {
READY: "ready",
};
}
constructor() {
super();
this._beat = new Beat();
this._create();
this.emit(Application.events.READY);
}
_create() {
const lyrics = ["Ah", "ha", "ha", "ha", "stayin' alive", "stayin' alive"];
let count = 0;
this._beat.on("update", (bit) => {
const message = document.createElement("div");
message.classList.add("message");
message.innerText = lyrics[bit];
document.querySelector(".main").appendChild(message);
})
}
}
Beat interval:
import EventEmitter from "eventemitter3";
export default class Beat extends EventEmitter{
static get events() {
return {
BIT: "bit",
};
}
constructor() {
super();
setInterval(() => {
console.log("bit");
this.emit(Beat.events.BIT);
}, 600);
}
}
You're subscribing to 'update' event, which is never fired.
Fixed code snippet:
this._beat.on(Beat.events.BIT, (bit) => {
const message = document.createElement("div");
message.classList.add("message");
message.innerText = lyrics[bit];
document.querySelector(".main").appendChild(message);
})