To create this effect you need to use JavaScript to dynamically change the text colour of some HTML many times a second using JavaScript’s setInterval() function. This function let’s you specify how many times per second a function is called – the examples here will call a function named flashtext() 20 times per second. The parameter 50 is passed tosetInterval() as this function requires the interval is set in milliseconds.

Before exploring the flashtext() function further here is the inital CSS for the text:

CSS Code

body {

background-color: #222222;

}

div {

font-family: Trebuchet MS, sans-serif;

font-size: 24px;

color: #EEEEEE;

}

Now we define the flashtext() function for random colours; every time a colour change is requested a hex colour is randomly created by selecting an element from an array that contains the letters A to F and the numbers 0 – 9 (the building blocks of hex colours). The actual colour is changed by directly accessing the color attribute of the style property usingdocument.getElementById().