JSFiddle - React, Tailwind, and code Playground
HTML
<span id=txt>My long text is long. My long text.My long text. My long text... My long text. My long text, so long...</span>
<br>Nb chars : <span id=mes></span>
CSS
#txt{ max-width:100px; overflow:hidden;height:20px;}
JavaScript
var text = $('#txt').text();
var nbchars = 0;
$('#txt').html('');
var increment = function() {
$('#txt').html(text.substring(0, nbchars));
if (nbchars<text.length-1 && $('#txt').width()<100) {
nbchars++;
window.setTimeout(increment, 1);
} else {
$('#mes').html(nbchars);
}
};
window.setTimeout(increment, 1);