From 245db33989f02513293b162695fac2114141bdc0 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Fri, 15 May 2020 18:00:06 +0100 Subject: [PATCH] chpwd.js: Only update DOM properties if they're actually going to change. This might have a significant effect on the background-friendliness of the validation machinery. Certainly, if I open Firefox's developer tools, I used to see the various `whinge' elements highlighted as changing all the time, which was rather distracting if nothing else, and probably meant that DOM change-handling machinery was being engaged in order to do nothing of any use. Introduce a new function `update' which changes an object property only if its value would actually change, and use this in the `check' function. --- chpwd.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/chpwd.js b/chpwd.js index a271db4..f771d65 100644 --- a/chpwd.js +++ b/chpwd.js @@ -64,10 +64,15 @@ var FORMS = {}; * submitting a form with invalid data. */ +function update(obj, slot, value) { + /* Update an object slot only if we're gping to change its value. */ + if (obj[slot] !== value) obj[slot] = value; +} + function check() { /* Check through the various forms to make sure they're filled in OK. If - * not, set the `F-whinge' elements, and disable `F-submit'. - */ + * not, set the `F-whinge' elements, and disable `F-submit'. + */ var f, form, whinge; for (f in FORMS) { @@ -75,10 +80,10 @@ function check() { we = elt(f + '-whinge'); sb = elt(f + '-submit'); whinge = form.check(); - if (sb !== null) sb.disabled = (whinge !== null); + if (sb !== null) update(sb, 'disabled', whinge !== null); if (we !== null) { - we.textContent = whinge || 'OK'; - we.className = whinge === null ? 'whinge' : 'whinge wrong'; + update(we, 'textContent', whinge || 'OK'); + update(we, 'className', whinge === null ? 'whinge' : 'whinge wrong'); } } -- 2.11.0