var LOGS_REFRESH_FREQUENCE = 2000; //in milliseconds var show_cagire = false; var show_telescope = false; var show_ddrago_b = false; var show_ddrago_r = false; var interval_cagire =-1; var interval_ddrago_r=-1; var interval_ddrago_b=-1; var interval_telescope=-1; jQuery(document).ready(function(){ /* The functions below are event watchers on the button corresponding to a device , it switch the value of the show variable of the device, then, depending of the new value of it, call to function to retrieve and display the logs or hide the logs and stop their retrieving */ $("#Telescope_button").click(function () { show_telescope= !show_telescope; if (show_telescope) { $("#telescope_list").show(); interval_telescope = setInterval(function() {ajax_request("telescope", "telescope_list");}, LOGS_REFRESH_FREQUENCE); } else { $("#telescope_list").hide(); if (interval_telescope !== -1){clearInterval(interval_telescope);} } }); $("#Cagire_button").click(function () { show_cagire = !show_cagire; if (show_cagire) { $("#cagire_list").show(); interval_cagire = setInterval(function() {ajax_request("cagire", "cagire_list");}, LOGS_REFRESH_FREQUENCE); } else { $("#cagire_list").hide(); if (interval_cagire !== -1){clearInterval(interval_cagire);} } }); $("#ddrago_r_button").click(function () { show_ddrago_r= !show_ddrago_r; if (show_ddrago_r) { $("#ddrago_r_list").show(); interval_ddrago_r = setInterval(function() {ajax_request("ddrago_r", "ddrago_r_list");}, LOGS_REFRESH_FREQUENCE); } else { $("#ddrago_r_list").hide(); if (interval_ddrago_r !== -1){clearInterval(interval_ddrago_r);} } }); $("#ddrago_b_button").click(function () { show_ddrago_b= !show_ddrago_b; if (show_ddrago_b) { $("#ddrago_b_list").show(); interval_ddrago_b = setInterval(function() {ajax_request("ddrago_b", "ddrago_b_list");}, LOGS_REFRESH_FREQUENCE); } else { $("#ddrago_b_list").hide(); if (interval_ddrago_b !== -1){clearInterval(interval_ddrago_b);} } }); /* function retriving logs for tools with show variable equal True, called every LOGS_REFRESH_FREQUENCE */ function ajax_request(cam, id_list){ var cagire_url ="/devices/update_cagire_logs"; var telescope_url ="/devices/update_telescope_logs"; var ddrago_r_url ="/devices/update_ddrago_r_logs"; var ddrago_b_url ="/devices/update_ddrago_b_logs"; var url_txt = ""; if (cam === 'cagire') url_txt = cagire_url; else if (cam === "telescope") url_txt = telescope_url; else if (cam === "ddrago_r") url_txt = ddrago_r_url; else if (cam === "ddrago_b") url_txt = ddrago_b_url; $.ajax({ url: url_txt, type: 'get', dataType: 'text', success: function(data) { var obj = JSON.parse(data); if (obj) { var listDiv = document.getElementById(id_list); listDiv.innerHTML = ''; for (var tmp in obj) { var li = document.createElement('li'); li.innerHTML = obj[tmp]['fields']['created'] + ": " + obj[tmp]['fields']['message']; // Use innerHTML to set the text listDiv.appendChild(li); } } }, error: function() { console.log('Ajax error: GET request failed\n'); } }); } }); $(window).unload(function(){ alert("bye") if (interval_cagire != -1){clearInterval(interval_cagire);} if (interval_telescope !== -1){clearInterval(interval_telescope);} });