From 131a27932946bac1fd3edbb47ba8015fd593b270 Mon Sep 17 00:00:00 2001 From: Stef van Buuren Date: Wed, 20 Mar 2024 16:26:42 +0100 Subject: [PATCH 1/7] Throttle radiobuttons agegrp, msr, etnicity, sex and agegrp_dsc --- inst/www/js/start.js | 2 +- inst/www/js/update.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/inst/www/js/start.js b/inst/www/js/start.js index 66769be..65bb87a 100644 --- a/inst/www/js/start.js +++ b/inst/www/js/start.js @@ -104,7 +104,7 @@ addChangeListener('chartgrp_dsc'); ["agegrp", "msr", "etnicity", "sex", "agegrp_dsc"].forEach(formName => { const radios = document.forms[formName].elements[formName]; for (let radio of radios) { - radio.onclick = update; + radio.onclick = throttledUpdate; } }); diff --git a/inst/www/js/update.js b/inst/www/js/update.js index 07a3d66..9394da4 100644 --- a/inst/www/js/update.js +++ b/inst/www/js/update.js @@ -67,6 +67,42 @@ function update() { }); } +/** + * Creates a throttled version of a function that only invokes the original + * function at most once per every wait milliseconds. + * + * @param {Function} func The function to throttle. + * @param {number} wait The number of milliseconds to throttle invocations to. + * @return {Function} A throttled version of the function. + */ +function throttle(func, wait) { + let isThrottling = false; + let lastArgs; + let lastThis; + + const invokeFunc = () => { + isThrottling = true; + setTimeout(() => { + isThrottling = false; + if (lastArgs) { + invokeFunc.apply(lastThis, lastArgs); + lastArgs = lastThis = null; + } + }, wait); + + func.apply(lastThis, lastArgs); + }; + + return function() { + if (!isThrottling) { + invokeFunc.apply(this, arguments); + } else { + lastArgs = arguments; + lastThis = this; + } + }; +} + function drawChart(params) { const rq = $("#plotDiv").rplot("draw_chart", params, session => { updateNoticePanel(2, session); @@ -81,3 +117,6 @@ function drawChart(params) { // Logging or user notification }); } + +// Create a throttled version of update that can only be called once every 1000 milliseconds (1 second) +const throttledUpdate = throttle(update, 1000); From ce701c10969925cc5acf918a838ff5dc1b559a6e Mon Sep 17 00:00:00 2001 From: Stef van Buuren Date: Wed, 20 Mar 2024 16:32:11 +0100 Subject: [PATCH 2/7] Set radio button throttle to 5 seconds --- inst/www/js/update.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/www/js/update.js b/inst/www/js/update.js index 9394da4..e9c2ea1 100644 --- a/inst/www/js/update.js +++ b/inst/www/js/update.js @@ -118,5 +118,5 @@ function drawChart(params) { }); } -// Create a throttled version of update that can only be called once every 1000 milliseconds (1 second) -const throttledUpdate = throttle(update, 1000); +// Create a throttled version of update that can only be called once every 5000 milliseconds (5 seconds) +const throttledUpdate = throttle(update, 5000); From 10cfa4008c635157ab59580dba244f8d585844c3 Mon Sep 17 00:00:00 2001 From: Stef van Buuren Date: Wed, 20 Mar 2024 17:44:11 +0100 Subject: [PATCH 3/7] Apply throttle to sliders and checkbox, transfer event listeners from HTML to JS --- inst/www/index.html | 20 ++++++++--------- inst/www/js/start.js | 52 +++++++++++++++++++++++++++++++++++++------ inst/www/js/update.js | 4 ++-- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/inst/www/index.html b/inst/www/index.html index 269122c..c48ac66 100644 --- a/inst/www/index.html +++ b/inst/www/index.html @@ -47,7 +47,7 @@
@@ -108,7 +108,7 @@


- + Curve interpolation
@@ -118,7 +118,7 @@
@@ -148,7 +148,7 @@


- + Curve interpolation
@@ -158,7 +158,7 @@
@@ -190,7 +190,7 @@
@@ -199,7 +199,7 @@