var notifications = { toFetch: true, unreadCount: 0, feeds: [] }; $('#menu .notification.icon.item').on('click', function (e) { e.preventDefault(); let el = e.currentTarget; let $wrapper = $('#notification-wrapper'); let $container = $('.notification.container'); if ($container.hasClass('active')) { // dismiss the notification window $('.notification.click.handler').remove(); } else { // render the notification window let handler = document.createElement('div') handler.className = "notification click handler"; $wrapper.after(handler); handler.addEventListener('click', function (e) { el.click(); }); setNotificationCounter(0); // remove counter if (notifications.toFetch){ fetchNotification(); } } el.classList.toggle('active'); $container.toggleClass('active'); }); function fetchNotificationCount() { axios.request({ method: 'GET', url: "./ajax/notification.php?count", responseType: 'json' }).then(function (res) { let count = res.data['unread_count']; setNotificationCounter(count); if (count != notifications.unreadCount) { // if count changes, fetching notifications while next click notifications.toFetch = true; notifications.unreadCount = count; } }).catch(function (_error) { console.error("Error occurred while fetching notification count."); }); } window.addEventListener('load', function () { fetchNotificationCount(); }); var notificationFetchTimer = setInterval(fetchNotificationCount, 1 * 5 * 1000); // fetch notification count every 1 minute function fetchNotification() { axios.request({ method: 'GET', url: './ajax/notification.php?fetch', responseType: 'json' }).then(function (res) { parseNotification(res.data); notifications.toFetch = false; }).catch(function (error) { console.log("Error occurred while fetching notification count."); }); } function parseNotification(data) { const feedTemplate = `