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.

JavaScript Callback Functions in JavaScript Callbacks and the DOM Callbacks with Arguments

Make 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);
}
Steven Parker
Steven Parker
221,293 Points

This 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.

2 Answers

Steven Parker
Steven Parker
221,293 Points

One 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 }
}

Okay 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
Steven Parker
221,293 Points

Switching 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);
}

I added a makeFlash function to the video's workspace in this snapshot that uses setInterval