From e22d9c86898a63cc2dd899c3682514785f326d4c Mon Sep 17 00:00:00 2001 From: Thomas Reese Date: Thu, 27 Aug 2020 18:39:34 -0700 Subject: [PATCH] Suggesting using addEventListener once option in docs Adding `{once : true}` to `node.addEventListener` causes the event trigger to be removed after first use. This built-in method cleans up the requirement to remove event within event handler. --- docsSource/sections/04-javascript.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docsSource/sections/04-javascript.md b/docsSource/sections/04-javascript.md index 9a9d170..a7208ec 100644 --- a/docsSource/sections/04-javascript.md +++ b/docsSource/sections/04-javascript.md @@ -37,14 +37,12 @@ const animateCSS = (element, animation, prefix = 'animate__') => node.classList.add(`${prefix}animated`, animationName); // When the animation ends, we clean the classes and resolve the Promise - function handleAnimationEnd() { + handleAnimationEnd = () => { node.classList.remove(`${prefix}animated`, animationName); - node.removeEventListener('animationend', handleAnimationEnd); - resolve('Animation ended'); } - node.addEventListener('animationend', handleAnimationEnd); + node.addEventListener('animationend', handleAnimationEnd, {once : true}); }); ```