function showNotification(message, avgReadingSpeed = 200) {
let notification = document.createElement("div");
notification.innerText = message;
notification.style.position = "fixed";
notification.style.top = "10px";
notification.style.left = "10px";
notification.style.backgroundColor = "rgba(0, 0, 0, 0.7)";
notification.style.color = "white";
notification.style.padding = "10px";
notification.style.borderRadius = "5px";
notification.style.zIndex = "10000";
notification.style.maxWidth = "90vw";
document.body.appendChild(notification);
let time = (function() {
let wordCount = message.trim().split(/\s+/).length;
let readingTimeMillis = (wordCount / avgReadingSpeed) * 60 * 1000;
return Math.max(readingTimeMillis, 3000);
})();
setTimeout(function() {
document.body.removeChild(notification);
}, time);
}
One thing to keep in mind with this function is that it does not check whether there is a notification already showing – so there is no graceful handling of stacking although that could be pretty easily implemented.