Live Badge Counts
Update notification badge counts in real time using polling or WebSockets with jQuery DOM updates.
Update notification badge counts in real time using polling or WebSockets with jQuery DOM updates.
function updateBadge($badge, count) {
if (count > 0) {
$badge.text(count > 99 ? "99+" : count).show();
$badge.closest("[aria-label]").attr("aria-label", `Notifications: ${count} unread`);
} else {
$badge.hide();
$badge.closest("[aria-label]").attr("aria-label", "Notifications: none");
}
}
// Poll every 30 seconds
setInterval(() => {
$.getJSON("/api/notifications/count", ({ unread }) => {
updateBadge($("#notif-badge"), unread);
});
}, 30000);
Update aria-label when the badge changes — screen reader users need to hear the new count.