Falsy comment in the word-count-web-component javascript file.
Vaatik opened this issue · 2 comments
In the word-count-web-component javascript file, there is a comment that goes as follows :
// Update count when element content changes
setInterval(function() {
var count = 'Words: ' + countWords(wcParent);
text.textContent = count;
}, 200)
This comments proves to be falsy, because the code running under it does not apply the new word count "when" the content changes. It applies the new word count every 200 milliseconds.
To make the code reflect the comment in a more truthful way, the code should instead listen for the input event on the parent node (the article itself) since the whole article is made editable via the contenteditable attribute in the html file.
it should go like this instead :
// Update count when element content changes
this.parentNode.addEventListener('input', () => {
var count = 'Words: ' + countWords(wcParent);
text.textContent = count;
});
This way, the code respects the comment more and performance is also better (even if this is a small demo, those principles should be important to always consider. Even more, since this is MDN and MDN is a reference for alot of dev coming from alot of ranges of experience)
The suggested enhancement makes sense to me.