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 to setInterval()
as this function requires the interval is set in milliseconds.
Before exploring the flashtext() function further here is the inital CSS for the text:
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 using
document.getElementById().
These resources are provided free of charge. If, however, you would like to make a donation to help finance these resources, no matter how big or small, please use the PayPal
button below.
Credits
This resource was written by Tim Bennett Bsc (Hons). Tim Bennett has a First Class Degree in
Multimedia Systems Computing
from Leeds Metropolitan University. He works as a freelance multimedia developer under the company name
Texelate creating free and
commissioned
high-quality multimedia solutions. Find out more by visiting the portfolio section and viewing his blog. If
you'd like to request a resource get in touch!
Rate this Resource
Click on the appropriate star below to rate this resource: