Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.