Batch click events from a color grid and flush on an interval
In this continuation of the Event Logger series, your task is to enhance the Event Logger system by integrating it with a user interface that generates events based on user interactions. Specifically, you’ll work with a grid of colored squares. Each time a square is clicked, an event should be logged, capturing essential details such as the event name, timestamp, and the color of the clicked square. A batch event logger like this can also use navigator.sendBeacon() to flush queued events when the page unloads.
The core functionality should be encapsulated within an EventLogger class. This class is responsible for batching events and sending them off at specified intervals, rather than sending each event immediately. This approach is crucial for optimizing performance and reducing network load, especially in scenarios with frequent user interactions.
The requirements for “Event Logger II” include:
EventLogger class that batches events and sends them at defined intervals.EventLogger should temporarily store events in a queue and only send them once the batch interval has elapsed, ensuring that events are sent in batches rather than individually.Example HTML structure for the grid of squares:
<div id="grid">
<div class="square" style="background-color: #ff5000;"></div>
<!-- Additional squares follow -->
</div>
Through “Event Logger II,” you’ll demonstrate the ability to handle user-generated events in a performant manner, leveraging batch processing to efficiently log and send event data.