Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

David Ryan
Courses Plus Student 14,981 PointsMake sirens?
Okay, so how would I make this into a bunch of flashing lights?
const div1 = document.getElementById('first');
const div2 = document.getElementById('second');
const div3 = document.getElementById('third');
function makeRed(element) {
element.style.backgroundColor = "red";
}
function makeWhite(element) {
element.style.backgroundColor = 'white';
}
function makeBlue(element) {
element.style.backgroundColor = "blue";
}
function addStyleToElement(element, callback) {
callback(element);
}
2 Answers

Steven Parker
221,293 PointsOne way to make things "flash" is to use CSS animation to change the opacity over time, and it requires no JavaScript. For example, the following code will make anything that has the class "blink" appear and disappear every second:
.blink {
animation: blink 1s linear infinite;
}
@keyframes blink {
50% { opacity: 1 }
51% { opacity: 0 }
100% { opacity: 0 }
}

David Ryan
Courses Plus Student 14,981 PointsOkay I see. I was figuring you could just create a function with timeInterval to switch the divs colors every second. This is from the callback functions lesson. I am a little confused on how to proceed with a function using the above code I pasted.
Thanks

Steven Parker
221,293 PointsSwitching colors is a job for JavaScript anyway. Here's a sample function that uses your code:
function switchColors(element) {
setInterval(function () {
if (element.style.backgroundColor == "red")
makeWhite(element);
else if (element.style.backgroundColor == "white")
makeBlue(element);
else
makeRed(element);
}, 1000);
}

KRIS NIKOLAISEN
54,739 PointsI added a makeFlash function to the video's workspace in this snapshot that uses setInterval
Steven Parker
221,293 PointsSteven Parker
221,293 PointsThis is just a few support routines, none of which are being called. Did you mean to post a more complete project including some HTML componen(s)? If possible, make a snapshot of your workspace and post the link to it here.