En action
↺ old fashionedcode
// Create a class for the elementclass WordCount {
constructor(element) {
this.elt = element;
this.parent = this.elt.parentNode;
// Update count when element content changes
setInterval( _=>this.append(),2000);
}
countWords(node){
const text = node.innerText || node.textContent;
return text.trim().split(/\s+/g).length;
}
append(){
this.elt.innerHTML="";
// Create text node and add word count to it
const text = document.createElement('span');
text.textContent = `Words: ${this.countWords(this.parent)}`;
// Append to the dom
this.elt.appendChild(text);
}
}
let t = new WordCount(document.getElementById("t"));
Aujourd'hui : Lit-html
https://lit-html.polymer-project.org/