Ask Your Question
2

Can we use JavaScript to create a typewriter effect with alternating words?

asked 2023-07-21 03:52:39 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-21 04:15:01 +0000

qstack gravatar image

Yes, it is possible to use JavaScript to create a typewriter effect with alternating words. Here is an example code snippet that uses JavaScript to achieve this effect:

<!-- HTML structure -->
<p id="typewriter"></p>

<!-- JavaScript code -->
<script>
    var words = ["Hello", "World", "How", "Are", "You"]; // array of words
    var i = 0; // index variable

    function typeWriter() {
        if (i == words.length) { i = 0; } // reset index when all words have been shown
        document.getElementById("typewriter").innerHTML = words[i]; // display current word
        i++; // move to next word
        setTimeout(typeWriter, 1000); // set time interval before displaying next word (in milliseconds)
    }

    typeWriter(); // call the function to start the typewriter effect
</script>

In this example, we have an array of words that we want to display in a typewriter-style effect, with each word appearing one at a time. The typeWriter() function uses a loop to display each word in the array, and the setTimeout() function sets a time interval of 1000ms (or 1 second) before displaying the next word. Once all words have been displayed, the index variable is reset to start the process over again. Finally, we call the typeWriter() function to start the typewriter effect.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-21 03:52:39 +0000

Seen: 17 times

Last updated: Jul 21 '23