class TextBlock {
constructor(id, text, delay) {
this.id = id;
this.text = text;
this.delay = delay;
this.timer = null;
}
async display() {
// Display the text
console.log(`Displaying block ${this.id}: ${this.text}`);
// Set a timer to hide the block after the delay
this.timer = setTimeout(() => {
this.hide();
}, this.delay);
}
hide() {
// Remove the block from the display
console.log(`Hiding block ${this.id}`);
clearTimeout(this.timer); // Clear the timer
this.timer = null;
}
}
class TextBlockManager {
constructor(rateLimit = 1000) {
this.textBlocks = [];
this.rateLimit = rateLimit;
this.nextId = 1;
}
addTextBlock(text, delay) {
const newBlock = new TextBlock(this.nextId++, text, delay);
this.textBlocks.push(newBlock);
return newBlock;
}
async run() {
for (const block of this.textBlocks) {
await new Promise(resolve => setTimeout(resolve, block.delay)); //Respect delay
block.display();
}
}
}
// Example Usage:
const manager = new TextBlockManager(500); // Rate limit of 500ms
const block1 = manager.addTextBlock("First block", 1000);
const block2 = manager.addTextBlock("Second block", 500);
const block3 = manager.addTextBlock("Third block", 750);
const block4 = manager.addTextBlock("Fourth block", 1200);
const block5 = manager.addTextBlock("Fifth block", 600);
manager.run();
Add your comment