From 4411f8cb37d1f0b17dacbfe3c02b84aea0940034 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 23 Mar 2016 08:54:48 +0000 Subject: [PATCH] Allow custom interval on AJAX sections of page MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app/assets/javascripts/updateContent.js | 11 +++++------ app/templates/views/dashboard/dashboard.html | 1 + 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/updateContent.js b/app/assets/javascripts/updateContent.js index 64cfc7325..4c78d4473 100644 --- a/app/assets/javascripts/updateContent.js +++ b/app/assets/javascripts/updateContent.js @@ -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 ); diff --git a/app/templates/views/dashboard/dashboard.html b/app/templates/views/dashboard/dashboard.html index d41e85f4d..411642b04 100644 --- a/app/templates/views/dashboard/dashboard.html +++ b/app/templates/views/dashboard/dashboard.html @@ -18,6 +18,7 @@ data-module="update-content" data-resource="{{url_for(".service_dashboard_updates", service_id=service_id)}}" data-key="today" + data-interval-seconds="10" > {% include 'views/dashboard/today.html' %}