Hello. The task is this: there are three blocks and it is necessary to change their protection correctly.
Here is what I tried to do:
$(document).ready(function() { red(); setTimeout(yellow(), 2000); }); function red() { var colors = [ "red", "black" ]; var el = $(".red"); var i = 0; setInterval(function() { el.css("background-color", colors[i]); if ((++i) >= 2) { i = 0; } }, 2000); } function yellow() { var colors = [ "yellow", "black" ]; var el = $(".yellow"); var i = 0; setInterval(function() { el.css("background-color", colors[i]); if ((++i) >= 2) { i = 0; } }, 1000); } .main_block { height: 500px; width: 240px; background-color: rgba(22, 22, 22, 0.93); position: relative; left: 50%; margin-left: -120px; border-radius: 75px; display: flex; flex-direction: column; } .red { background-color: #000000; } .yellow { background-color: #000000; } .green { background-color: #000000; } .red, .yellow, .green { width: 100px; height: 100px; border-radius: 50px; position: relative; left: 50%; margin: 50px 0 0 -50px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="main_block"> <div class="red"></div> <div class="yellow"></div> <div class="green"></div> </div> The algorithm should be as follows:
- Red lights up for 2 seconds
- Yellow - 1 second
- Green - 3 seconds
But I got confused with setInterval and setTimeout.
I would be grateful if someone tells me the algorithm for doing all this. Those. All these delays should be correctly described, but I do not understand how
Thanks for attention