From 8cd3a480a7388877628ce8b9b8e50cd5df48bb81 Mon Sep 17 00:00:00 2001 From: Mark Croxton Date: Tue, 28 Mar 2023 20:28:39 +0100 Subject: [PATCH] Nullify unreachable references to dom nodes inside setTimeout(), to allow garbage collection. (#1330) --- src/htmx.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/htmx.js b/src/htmx.js index 05c9a8d9c..0d599e75a 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -471,7 +471,10 @@ return (function () { function removeElement(elt, delay) { elt = resolveTarget(elt); if (delay) { - setTimeout(function(){removeElement(elt);}, delay) + setTimeout(function(){ + removeElement(elt); + elt = null; + }, delay); } else { elt.parentElement.removeChild(elt); } @@ -480,7 +483,10 @@ return (function () { function addClassToElement(elt, clazz, delay) { elt = resolveTarget(elt); if (delay) { - setTimeout(function(){addClassToElement(elt, clazz);}, delay) + setTimeout(function(){ + addClassToElement(elt, clazz); + elt = null; + }, delay); } else { elt.classList && elt.classList.add(clazz); } @@ -489,7 +495,10 @@ return (function () { function removeClassFromElement(elt, clazz, delay) { elt = resolveTarget(elt); if (delay) { - setTimeout(function(){removeClassFromElement(elt, clazz);}, delay) + setTimeout(function(){ + removeClassFromElement(elt, clazz); + elt = null; + }, delay); } else { if (elt.classList) { elt.classList.remove(clazz); @@ -3410,6 +3419,7 @@ return (function () { }; setTimeout(function () { triggerEvent(body, 'htmx:load', {}); // give ready handlers a chance to load up before firing this event + body = null; // kill reference for gc }, 0); })