Check the code bellow. Here i am trying to animate a text which will be iterate over and over again without click button. First part of code works on click button but second part not works where i removed function and added it in jquery ready() method. It should be work on document ready but its not working. Whats wrong i am doing here?
This is what working now on click button:
<h1>Typewriter</h1>
<button onclick="typeWriter()">Click me</button>
<p id="demo"></p>
<script>
var i = 0;
var txt = 'Lorem ipsum dummy text blabla.';
var speed = 50;
function typeWriter() {
if (i < txt.length) {
document.getElementById("demo").innerHTML += txt.charAt(i);
i++;
setTimeout(typeWriter, speed);
}
}
</script>
This is what i tried already:
<span id="demo"></span>
$(document).ready(function () {
typeWriter();
//writing effect
var i = 0;
var txt = 'Lorem ipsum dummy text blabla.';
var speed = 50;
function typeWriter() {
if (i < txt.length) {
document.getElementById("demo").innerHTML += txt.charAt(i);
i++;
setTimeout(typeWriter, speed);
}
}
});