Files
notifications-admin/app/assets/javascripts/updateContent.js
Chris Hill-Scott 4411f8cb37 Allow custom interval on AJAX sections of page
Some pages with AJAX should update quickly, because the data is likely
to be changing quickly, and be finished changing sooner. Other pages we
want to have tick over a bit slower.

This commit adds an optional ‘interval’ parameter to the updateContent
modules, which sets how often the page should ping the server for an
update.

It then sets the interval for the dashboard page to be 10 seconds,
rather than the default 1.5 seconds.
2016-03-23 13:40:23 +00:00

44 lines
1013 B
JavaScript

(function(GOVUK, Modules) {
"use strict";
GOVUK.timeCache = {};
GOVUK.resultCache = {};
let getter = function(resource, interval, render) {
if (
GOVUK.resultCache[resource] &&
(Date.now() < GOVUK.timeCache[resource])
) {
render(GOVUK.resultCache[resource]);
} else {
GOVUK.timeCache[resource] = Date.now() + interval;
$.get(
resource,
response => render(GOVUK.resultCache[resource] = response)
);
}
};
let poller = (resource, key, component, interval) => () => getter(
resource, interval, response => component.html(response[key])
);
Modules.UpdateContent = function() {
this.start = function(component) {
const $component = $(component);
interval = ($(component).data("interval-seconds") * 1000) || 1500;
setInterval(
poller($component.data('resource'), $component.data('key'), $component, interval),
interval / 5
);
};
};
})(window.GOVUK, window.GOVUK.Modules);