I want to create a type writer effect on a paragraph and repeat it after every 10 seconds but i don't want it to be repeated if the screen size is less than 768px?
I have created a typewriter function which prints words slowly using a setTimeout() function and when the whole paragraph is printed it waits for 5 seconds again starts printing from the beginning.
I wrote this code, but it is not working.
<!DOCTYPE html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
.about p{
color: #000;
font-size: 22px;
}
</style>
</head>
<body>
<div class="about" onload="typeWriter();">
<p id="text"></p>
</div>
<script>
var i = 0;
var w = window.matchMedia("(min-width: 768px)");
var txt = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ';
var speed = 50;
function typeWriter() {
if (i < txt.length) {
document.getElementById("text").innerHTML += txt.charAt(i);
i++;
setTimeout(typeWriter, speed);
}
else{
rep(w);
w.addListener(rep);
}
}
function rep(w) {
if (w.matches) {
setTimeout(function(){
i = 0;
speed = 50;
document.getElementById("text").innerHTML = '';
setTimeout(typeWriter, speed);
}, 5000);
}
}
</script>
</body>
</html>