Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
230,024 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
230,024 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
230,024 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