async.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="./style.css">
  7. <title>Traffic light</title>
  8. </head>
  9. <body>
  10. <div id="traffic-lightS">
  11. <div id="traffic-light" class="lights">
  12. <div id="red" class="oneColor"></div>
  13. <div id="yellow" class="oneColor"></div>
  14. <div id="green" class="oneColor"></div>
  15. </div>
  16. <div id="pedestrian-light" class="lights">
  17. <div id="pedestrian-red" class="oneColor"></div>
  18. <!-- <div id="pedestrian-yellow" class="oneColor"></div> -->
  19. <div id="pedestrian-green" class="oneColor"></div>
  20. </div>
  21. </div>
  22. <button id="knopka">knopka</button>
  23. <script>
  24. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  25. let red = document.getElementById("red");
  26. let yellow = document.getElementById("yellow");
  27. let green = document.getElementById("green");
  28. let pedRed = document.getElementById("pedestrian-red");
  29. let pedYellow = document.getElementById("pedestrian-yellow");
  30. let pedGreen = document.getElementById("pedestrian-green");
  31. let redlight;
  32. let pressed = false;
  33. async function trafficLight(){
  34. while (true){
  35. red.style = "background-color: red";
  36. let redSec = 5;
  37. for (let i = redSec; i > 0; i--) {
  38. red.innerText = redSec;
  39. redSec -=1;
  40. await delay(1000);
  41. }
  42. yellow.style = "background-color: yellow";
  43. red.innerText = ""
  44. await delay(500);
  45. red.style = "background-color: black";
  46. await delay(500);
  47. yellow.style = "background-color: black"
  48. green.style = "background-color: green";
  49. let greenSec = 20;
  50. for(let i = greenSec; i > 0; i--) {
  51. green.innerText = greenSec;
  52. await delay(1000);
  53. greenSec -=1;
  54. }
  55. yellow.style = "background-color: yellow";
  56. green.innerText = "";
  57. await delay(500);
  58. green.style = "background-color: black";
  59. await delay(500);
  60. yellow.style = "background-color: black";
  61. }
  62. }
  63. trafficLight()
  64. async function pedestrianTrafficLight(msc){
  65. while(true) {
  66. if(redlight == false) {
  67. redlight = true;
  68. pedRed.style = "background-color: red";
  69. pedGreen.style = "background-color: black";
  70. await delay(21000)
  71. } else {
  72. redlight = false;
  73. pedRed.style = "background-color: black";
  74. pedGreen.style = "background-color: green";
  75. await delay(6000);
  76. }
  77. }
  78. }
  79. pedestrianTrafficLight()
  80. function domEventPromise(element, eventName) {
  81. return new Promise(resolve => {
  82. element.addEventListener(eventName, ev => {
  83. resolve(ev)
  84. element.removeEventListener(eventName, this)
  85. });
  86. })
  87. }
  88. async function greenOn() {
  89. if(redlight == true) {
  90. pedRed.style = "background-color: black";
  91. pedGreen.style = "background-color: green";
  92. red.style = "background-color: red";
  93. green.style = "background-color: black; color: black";
  94. let redSec = 5;
  95. for (let i = redSec; i > 0; i--) {
  96. red.innerText = redSec;
  97. redSec -=1;
  98. await delay(1000);
  99. }
  100. //await delay(4000);
  101. red.innerText = "";
  102. yellow.style = "background-color: yellow";
  103. await delay(500);
  104. red.style = "background-color: black";
  105. await delay(500);
  106. yellow.style = "background-color: black";
  107. green.style = "background-color: green";
  108. pedRed.style = "background-color:red";
  109. pedGreen.style = "background-color: black";
  110. }
  111. }
  112. setInterval(() => {
  113. domEventPromise(knopka, 'click').then( e => addEventListener("click", greenOn()))
  114. }, 20000);
  115. </script>
  116. </body>
  117. </html>