dejwux
dejwux

Reputation: 1

Javascript - setTimeout

I am learning Javascript right now. I have a small issue that I can't figure out how to solve it. I would like to clear content of my html page after my function displayed "Hi hi" in web page.

<html>
<body onload="alertFunc()">

<script>

function alertFunc() {
  var statement = "Hi hi"
  for (let i = 0; i < statement.length; i++) {
  let c = statement.charAt(i);

  setTimeout(function(){
   document.write(c);
},i * 1000);

}

}

</script>

</body>
</html>

Upvotes: 0

Views: 64

Answers (1)

samair ali
samair ali

Reputation: 802

try this to clear content of your site after 1 second

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Learning </title>
</head>
<body>
    <script>
        document.write('hi hi');
        function alertFunc() {


        setTimeout(function(){
            document.write(' ');
        }, 1000);

        }
        alertFunc();

    </script>
</body>
</html>

if you want to change content with time again and again then you have to use setInterval

Upvotes: 1

Related Questions