What is the most elegant way of showing an html text letter by letter (like videogame captions) using CSS and JavaScript?
While I\'m sure that his can be solved usin
Make your code more elegant by preparing promises for each iteration, then execute them as second step where you can inject DOM logic.
const message = 'Solution using Promises';
const typingPromises = (message, timeout) =>
[...message].map(
(ch, i) =>
new Promise(resolve => {
setTimeout(() => {
resolve(message.substring(0, i + 1));
}, timeout * i);
})
);
typingPromises(message, 140).forEach(promise => {
promise.then(portion => {
document.querySelector('p').innerHTML = portion;
});
});