async2.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 light2</title>
  8. </head>
  9. <body>
  10. <div class="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-green" class="oneColor"></div>
  19. </div>
  20. </div>
  21. <button id="knopka">knopka</button>
  22. <div class="traffic-lightS">
  23. <div id="pedestrian-light-async" class="lights">
  24. <div id="pedestrian2-red" class="oneColor"></div>
  25. <div id="pedestrian2-green" class="oneColor"></div>
  26. </div>
  27. </div>
  28. <button id="promise">promise</button>
  29. <script>
  30. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  31. let red = document.getElementById("red");
  32. let yellow = document.getElementById("yellow");
  33. let green = document.getElementById("green");
  34. let pedRed = document.getElementById("pedestrian-red");
  35. let pedYellow = document.getElementById("pedestrian-yellow");
  36. let pedGreen = document.getElementById("pedestrian-green");
  37. let redlight;
  38. let pedRed2 = document.getElementById("pedestrian2-red");
  39. let pedGreen2 = document.getElementById("pedestrian2-green");
  40. let promise = document.getElementById("promise")
  41. let buttonPushed = false;
  42. async function trafficLight(){
  43. while (true){
  44. red.style = "background-color: red";
  45. let redSec = 5;
  46. for (let i = redSec; i > 0; i--) {
  47. red.innerText = redSec;
  48. redSec -=1;
  49. await delay(1000);
  50. }
  51. yellow.style = "background-color: yellow";
  52. red.innerText = ""
  53. await delay(500);
  54. red.style = "background-color: black";
  55. await delay(500);
  56. yellow.style = "background-color: black"
  57. green.style = "background-color: green";
  58. let greenSec = 20;
  59. for(let i = greenSec; i > 0; i--) {
  60. green.innerText = greenSec;
  61. await delay(1000);
  62. greenSec -=1;
  63. }
  64. yellow.style = "background-color: yellow";
  65. green.innerText = "";
  66. await delay(500);
  67. green.style = "background-color: black";
  68. await delay(500);
  69. yellow.style = "background-color: black";
  70. }
  71. }
  72. trafficLight()
  73. async function pedestrianTrafficLight(msc){
  74. while(true) {
  75. if(redlight == false) {
  76. redlight = true;
  77. pedRed.style = "background-color: red";
  78. pedGreen.style = "background-color: black";
  79. await delay(21000)
  80. } else {
  81. redlight = false;
  82. pedRed.style = "background-color: black";
  83. pedGreen.style = "background-color: green";
  84. await delay(6000);
  85. }
  86. }
  87. }
  88. pedestrianTrafficLight()
  89. function domEventPromise(element, eventName) {
  90. return new Promise(resolve => {
  91. element.addEventListener(eventName, ev => {
  92. resolve(ev)
  93. element.removeEventListener(eventName, this)
  94. });
  95. })
  96. }
  97. async function greenOn() {
  98. if(redlight == true) {
  99. pedRed.style = "background-color: black";
  100. pedGreen.style = "background-color: green";
  101. red.style = "background-color: red";
  102. green.style = "background-color: black; color: black";
  103. let redSec = 5;
  104. for (let i = redSec; i > 0; i--) {
  105. red.innerText = redSec;
  106. redSec -=1;
  107. await delay(1000);
  108. }
  109. red.innerText = "";
  110. yellow.style = "background-color: yellow";
  111. await delay(500);
  112. red.style = "background-color: black";
  113. await delay(500);
  114. yellow.style = "background-color: black";
  115. green.style = "background-color: green";
  116. pedRed.style = "background-color:red";
  117. pedGreen.style = "background-color: black";
  118. }
  119. }
  120. setInterval(() => {
  121. domEventPromise(knopka, 'click').then( e => addEventListener("click", greenOn()))
  122. }, 20000);
  123. async function PromiseTraffic() {
  124. var flag = true;
  125. let timeDelay = 15000;
  126. while(true) {
  127. pedRed2.style = "background-color: red";
  128. pedGreen2.style = "background-color: black";
  129. if(flag) {
  130. var res = await Promise.race([delay(timeDelay), domEventPromise2(promise, 'click')]);
  131. } else {
  132. var res = await delay(timeDelay);
  133. }
  134. if(res != timeDelay) {
  135. flag = false;
  136. setTimeout(() => {
  137. flag = true;
  138. }, 20000);
  139. }
  140. pedRed2.style = "background-color: black";
  141. pedGreen2.style = "background-color: green";
  142. await delay(5000);
  143. }
  144. }
  145. PromiseTraffic()
  146. function domEventPromise2(element, eventName) {
  147. return new Promise(resolve => {
  148. var fnc = event => {
  149. resolve(event)
  150. element.removeEventListener(eventName, fnc)
  151. };
  152. element.addEventListener(eventName, fnc);
  153. })
  154. }
  155. </script>
  156. </body>
  157. </html>