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.
This commit is contained in:
Chris Hill-Scott
2016-03-23 08:54:48 +00:00
parent 5c57f5b588
commit 4411f8cb37
2 changed files with 6 additions and 6 deletions

View File

@@ -1,12 +1,10 @@
(function(GOVUK, Modules) {
"use strict";
const interval = 1500; // milliseconds
GOVUK.timeCache = {};
GOVUK.resultCache = {};
let getter = function(resource, render) {
let getter = function(resource, interval, render) {
if (
GOVUK.resultCache[resource] &&
@@ -23,8 +21,8 @@
};
let poller = (resource, key, component) => () => getter(
resource, response => component.html(response[key])
let poller = (resource, key, component, interval) => () => getter(
resource, interval, response => component.html(response[key])
);
Modules.UpdateContent = function() {
@@ -32,9 +30,10 @@
this.start = function(component) {
const $component = $(component);
interval = ($(component).data("interval-seconds") * 1000) || 1500;
setInterval(
poller($component.data('resource'), $component.data('key'), $component),
poller($component.data('resource'), $component.data('key'), $component, interval),
interval / 5
);