I have Django app with a plot that is showing the current CPU/RAM usage. This script makes a GET call every second to retrieve information gathered by Celery. I am trying to add an clearInterval()
call everytime the tab with the app is hidden to minimize the ammount of GETs.
I have tried to add a js_on_event
call to the source
but it does nothing (i don’t even get the console logs). Adding the custom JS to html page works, but I dont have reference to the plot objects (source).
source
source = AjaxDataSource(
# Initial data provided to make axes generate properly at the start.
data=theDataModel.method_charting_data(),
data_url='/path/to/ask/?period=minute',
polling_interval=1000,
method='GET'
)
source.js_on_event('document_visibilitychange', CustomJS(
args={'periodSelect': variable_for_selectWidget},
code=static_file('path/to/js/visibility_change.js')
))
js_on_event CustomJS
document.addEventListener('visibilitychange', function () {
if (document.hidden) {
clearInterval(this.interval);
console.log(document.visibilityState);
} else {
var intervalMap = {
'minute': 1000,
'hour': 1000*60,
'day': null,
'month': null
};
this.period = periodSelect.value;
this.data_url = `/path/to/ask/?period=${this.period}`;
this.polling_interval = intervalMap[periodSelect.value];
const callback = () => this.get_data(this.mode, this.max_size, this.if_modified);
callback();
if (this.polling_interval != null) {
this.interval = setInterval(callback, this.polling_interval);
}
console.log(document.visibilityState);
}
});
I have also tried the code without the document.addEventListener('visibilitychange', function () {
part
There are a number of on_change
events added to tools, but as the name implies, they are on change and two of them refer to the source