From e6595562fa408ee0a20985d8bb2a37500f313aaf Mon Sep 17 00:00:00 2001 From: Jorran de Wit <jorrandewit@outlook.com> Date: Thu, 5 Oct 2017 21:18:45 +0200 Subject: [PATCH] Fix: notification counter used extreme traffic There was a bug in the js, resulting in a new loop starting every time one click on the notifications popup or one of its links. Only a browser refresh would've fixed it, potentially resulting in a inifinite number of refreshes per second!!! --- .../static/scipost/assets/js/notifications.js | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/scipost/static/scipost/assets/js/notifications.js b/scipost/static/scipost/assets/js/notifications.js index 6f85c449b..5900e9edd 100644 --- a/scipost/static/scipost/assets/js/notifications.js +++ b/scipost/static/scipost/assets/js/notifications.js @@ -5,7 +5,7 @@ var notify_api_url_list = "/notifications/api/list/"; var notify_api_url_toggle_read = "/notifications/mark-toggle/"; var notify_api_url_mark_all_read = "/notifications/mark-all-as-read/"; var notify_fetch_count = "5"; -var notify_refresh_period = 15000; +var notify_refresh_period = 60000; var consecutive_misfires = 0; var registered_functions = [fill_notification_badge]; @@ -91,7 +91,7 @@ function fill_notification_badge(data) { } function get_notification_list() { - var data = fetch_api_data(notify_api_url_list, function(data) { + fetch_api_data(notify_api_url_list, true, function(data) { var messages = data.list.map(function (item) { var message = ''; @@ -128,10 +128,13 @@ function get_notification_list() { }); } -function fetch_api_data(url, callback) { +function fetch_api_data(url, once, callback) { if (!url) { var url = notify_api_url_count; } + if (!once) { + var once = false; + } if (registered_functions.length > 0) { //only fetch data if a function is setup @@ -153,17 +156,28 @@ function fetch_api_data(url, callback) { r.open("GET", url + '?max=' + notify_fetch_count, true); r.send(); } - if (consecutive_misfires < 10) { - setTimeout(fetch_api_data,notify_refresh_period); - } else { - var badges = document.getElementsByClassName(notify_badge_class); - if (badges) { - for (var i=0; i < badges.length; i++){ - badges[i].innerHTML = "!"; - badges[i].title = "Connection lost!" + var timer = null; + if (!once) { + if (consecutive_misfires < 10 && !once) { + timer = setTimeout(fetch_api_data, notify_refresh_period); + } else { + var badges = document.getElementsByClassName(notify_badge_class); + if (badges) { + for (var i=0; i < badges.length; i++){ + badges[i].innerHTML = "!"; + badges[i].title = "Connection lost!" + } } } } + + return stop; + function stop() { + if (timer) { + clearTimeout(timer); + timer = 0; + } + } } setTimeout(fetch_api_data, 1000); -- GitLab