HW18_svetofor.html 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. #traffic-light-body {
  10. border: 2px solid black;
  11. padding: 20px;
  12. border-radius: 50px;
  13. width: min-content;
  14. }
  15. #color-red,
  16. #color-yellow,
  17. #color-green {
  18. width: 100px;
  19. height: 100px;
  20. border-radius: 50%;
  21. border: 10px solid black;
  22. margin-bottom: 10px;
  23. background-color: transparent;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="traffic-light">
  29. <div id="color-red">
  30. </div>
  31. <div id="color-yellow">
  32. </div>
  33. <div id="color-green">
  34. </div>
  35. </div>
  36. </body>
  37. <script>
  38. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  39. async function trafficLight(timer) {
  40. while (true) {
  41. const red = document.getElementById('color-red')
  42. red.style.backgroundColor = 'red'
  43. await delay(timer)
  44. const yellow = document.getElementById('color-yellow')
  45. yellow.style.backgroundColor = 'yellow'
  46. await delay(timer)
  47. red.style.backgroundColor = yellow.style.backgroundColor = 'transparent'
  48. const green = document.getElementById('color-green')
  49. green.style.backgroundColor = 'green'
  50. await delay(timer)
  51. green.style.backgroundColor = 'transparent'
  52. }
  53. }
  54. trafficLight(1500)
  55. </script>
  56. </html>