Open In App

Printing the table on the web page having type effect repeatedly

In this article, we will see how to print the table on the web page having type effect repeatedly using HTML, CSS & Javascript. Here, the printing of the table with the type effect can be done with the JavaScript setTimeout() function that helps to set the timer to run the function or piece of code once the timer expires. 

Syntax: The format for printing the table will be as given below: 



n * 1 = n
n * 2 = 2n
...

Approach: The below approach will be implemented to print the table with type effect:

Example: This example illustrates printing the table on the web page having a type effect repeatedly using HTML, CSS & JS.






<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport"
          content="width=device-width, 
                         initial-scale=1.0" />
    <title>
        Printing the table on the web page
        having type effect repeatedly
    </title>
  
    <style>
        * {
            box-sizing: border-box;
        }
  
        h1 {
            color: green;
        }
  
        #message {
            display: flex;
            flex-direction: row;
            width: 400px;
            height: 400px;
            border: 2px solid royalblue;
            border-radius: 10px;
            color: royalblue;
            font-size: x-large;
            justify-content: center;
            padding: 30px;
            margin-left: 40px;
        }
  
        .common {
            width: 400px;
            height: 60px;
            margin: 10px 10px 20px 40px;
            background-color: rgba(0, 120, 0, 1);
            border-radius: 15px;
            border-color: rgba(0, 120, 0, 1);
            font-size: larger;
            color: white;
        }
  
        #start:hover {
            background-color: rgba(0, 160, 0, 1);
        }
  
        #stop:hover {
            background-color: rgba(0, 160, 0, 1);
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h3>
        Printing the Table With Type Effect
    </h3>
    <button id="start" 
            class="common">
            Start Printing
    </button>
    <div id="message"></div>
    <script>
        const messageContainer = 
            document.getElementById("message");
        const startButtton = 
            document.getElementById("start");
        const typeSpeed = 150;
        let count = 1;
        let index = 0;
        function typeEffect(value) {
  
            // Set of values that will be displayed in the row
            let arr = 
                [value, " * ", count, " = ", value * count, "<br>"];
            if (count >= 1 && count <= 10) {
                  
                // Appending the table inside the div element
                // having id messageContainer
                messageContainer.innerHTML += `${arr[index]} `;
                index++;
                index = index % arr.length;
                if (index == 0) {
                    count++;
                }
                setTimeout(function () {
                    typeEffect(value);
                }, typeSpeed);
            }
            if (count == 11) {
                count = 1;
                messageContainer.textContent = "";
            }
        }
        startButtton.addEventListener("click", function (e) {
            typeEffect(value = 10);
        })
    </script>
</body>
  
</html>

Output:

 


Article Tags :