typwriter effect ks code example
Example 1: javascript typewriter effect
let i = 0;
let target = document.getElementById("target");
let text = target.innerHTML;
target.innerHTML = ' ';
let speed = 75;
typeWriter();
function typeWriter() {
if (i < text.length) {
target.innerHTML += text.charAt(i);
i++;
setTimeout(typeWriter, speed);
}
}
Example 2: javascript text auto writing
<p id="demo"></p>
<button onclick="typeWriter()">Click me</button>
var i = 0;
var txt = 'Lorem ipsum typing effect!';
var speed = 50;
function typeWriter() {
if (i < txt.length) {
document.getElementById("demo").innerHTML += txt.charAt(i);
i++;
setTimeout(typeWriter, speed);
}
}