use setTimeout instead of setInterval to avoid flooding the server when the API needs some time to answer

This commit is contained in:
ziirish 2018-01-16 11:08:26 +01:00
parent e83c45c7c5
commit 65a66d9241
No known key found for this signature in database
GPG key ID: 72DB229A64B54E46
3 changed files with 30 additions and 20 deletions

View file

@ -469,7 +469,8 @@ $(function() {
* auto-refresh our page if needed
*/
{% set autorefresh = config.REFRESH -%}
var auto_refresh = setInterval(function() {
var auto_refresh = undefined;
var auto_refresh_function = function() {
{% if clients -%}
_clients();
{% endif -%}
@ -479,8 +480,12 @@ $(function() {
{% if servers and overview -%}
_servers();
{% endif -%}
return;
}, {{ autorefresh * 1000 }});
if (auto_refresh) {
clearTimeout(auto_refresh);
}
auto_refresh = setTimeout(auto_refresh_function, {{ autorefresh * 1000 }});
};
auto_refresh = setTimeout(auto_refresh_function, {{ autorefresh * 1000 }});
{% endif -%}
{% if not login -%}
@ -488,9 +493,15 @@ $(function() {
/***
* Javascript Loop
*/
var refresh_running = setInterval(function () {
var refresh_running = undefined;
var refresh_function = function() {
_check_running();
}, {{ config.LIVEREFRESH * 1000 }});
if (refresh_running) {
clearTimeout(refresh_running);
}
refresh_running = setTimeout(refresh_function, {{ config.LIVEREFRESH * 1000 }});
};
refresh_running = setTimeout(refresh_function, {{ config.LIVEREFRESH * 1000 }});
{% endif -%}
{% endif -%}
});

View file

@ -195,7 +195,7 @@ var refresh_status = function( is_running ) {
var _span = $('#running-status');
var _inner_format_status = function(status) {
var _content = '<span class="'+__icons[status.state]+'" aria-hidden="true"></span> ';
if (status.state == 'running') {
if (status.state == '{{ _("running") }}') {
_client_running = true;
_content += status.state+' - '+status.phase;
if (status.percent > 0) {
@ -227,11 +227,11 @@ var refresh_status = function( is_running ) {
_get_running = _inner_get_status();
}
var _inner_callback_setup = function() {
if (__refresh_running && !(is_running || _client_running)) {
clearInterval(__refresh_running);
__refresh_running = undefined;
} else if (!__refresh_running && _client_running) {
__refresh_running = setInterval(function() {
if (__refresh_running) {
clearTimeout(__refresh_running);
}
if (_client_running) {
__refresh_running = setTimeout(function() {
refresh_status(true);
}, {{ config.LIVEREFRESH * 1000 }});
}

View file

@ -196,17 +196,16 @@ var refresh_status = function( is_running ) {
});
_promises.push(_p);
});
if (__refresh_running && (!is_running || _clients_running.length == 0)) {
// stop loop if no more clients are running
clearInterval(__refresh_running);
__refresh_running = undefined;
} else if (!__refresh_running && _clients_running.length > 0) {
__refresh_running = setInterval(function() {
refresh_status(true);
}, {{ config.LIVEREFRESH * 1000 }});
}
$.when.apply( $, _promises ).done( function() {
_clients_table.draw(false);
if (_clients_running.length > 0) {
if (__refresh_running) {
clearTimeout(__refresh_running);
}
__refresh_running = setTimeout(function() {
refresh_status(true);
}, {{ config.LIVEREFRESH * 1000 }});
}
});
};
if (_get_running) {