Browse Source

<hw_async2> done

Mark 1 year ago
parent
commit
bd6b131c97
2 changed files with 118 additions and 0 deletions
  1. 39 0
      14/index.html
  2. 79 0
      14/main.js

+ 39 - 0
14/index.html

@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+   <meta charset="UTF-8">
+   <meta http-equiv="X-UA-Compatible" content="IE=edge">
+   <meta name="viewport" content="width=device-width, initial-scale=1.0">
+   <title>Document</title>
+
+</head>
+
+<body>
+   <div id="container" style="width: 100px; display:flex; flex-direction:column;">
+      <div class="red"
+         style="background-color:red;width: 100%;height: 100px;border: 2px solid black; border-radius: 50%;">
+      </div>
+      <div class="yellow"
+         style="background-color:yellow;width: 100%;height: 100px;border: 2px solid black; border-radius: 50%;">
+      </div>
+      <div class="green"
+         style="background-color:green;width: 100%;height: 100px;border: 2px solid black; border-radius: 50%;">
+      </div>
+   </div>
+   <button class="btn">клик</button>
+   <div id="container1" style="width: 100px; display:flex; flex-direction:column;">
+      <div class="red1"
+         style="background-color:red;width: 100%;height: 100px;border: 2px solid black; border-radius: 50%;">
+      </div>
+
+      <div class="green1"
+         style="background-color:green;width: 100%;height: 100px;border: 2px solid black; border-radius: 50%;">
+      </div>
+      <button class="btn1">перейти дорогу</button>
+   </div>
+
+   <script src="main.js"></script>
+</body>
+
+</html>

+ 79 - 0
14/main.js

@@ -0,0 +1,79 @@
+// Async/Await Homework 2
+// Светофор
+const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
+const red = document.querySelector('.red')
+const yellow = document.querySelector('.yellow')
+const green = document.querySelector('.green')
+
+async function trafficLight() {
+   while (true) {
+      changeLight(green)
+      await delay(3000)
+      changeLight(red)
+      await delay(3000)
+      changeLight(yellow)
+      await delay(3000)
+   }
+}
+trafficLight()
+
+function changeLight(color) {
+   if (color !== red && color !== yellow) {
+      red.style.opacity = .2
+      yellow.style.opacity = .2
+      green.style.opacity = 1
+   } if (color !== red && color !== green) {
+      red.style.opacity = .2
+      yellow.style.opacity = 1
+      green.style.opacity = .2
+   }
+   if (color !== yellow && color !== green) {
+      red.style.opacity = 1
+      yellow.style.opacity = .2
+      green.style.opacity = .2
+   }
+}
+
+// PedestrianTrafficLight
+const red1 = document.querySelector('.red1')
+const green1 = document.querySelector('.green1')
+let btn1 = document.querySelector('.btn1')
+async function PedestrianTrafficLight() {
+   while (true) {
+      changePedestrianTrafficLight(red1)
+      await Promise.race([delay(4100), new Promise((ok) => btn1.onclick = ok)])
+      changePedestrianTrafficLight(green1)
+      await Promise.race([delay(4100), new Promise((ok) => btn1.onclick = ok)])
+   }
+}
+PedestrianTrafficLight()
+
+btn1.addEventListener('click', () => {
+   changeLight(red);
+   changePedestrianTrafficLight(green1);
+   btn1.disabled = true;
+   setTimeout(() => { btn1.disabled = false }, 10000);
+})
+
+function changePedestrianTrafficLight(color1) {
+   if (color1 !== red1) {
+      red1.style.opacity = .2
+      green1.style.opacity = 1
+   }
+   if (color1 !== green1) {
+      red1.style.opacity = 1
+      green1.style.opacity = .2
+   }
+}
+
+// domEventPromise
+let btn = document.querySelector('.btn')
+function domEventPromise(button, eventName) {
+   return new Promise(res => {
+      button.addEventListener(eventName, (e) => res(e))
+      button.removeEventListener(eventName, (e) => res(e))
+   });
+}
+domEventPromise(btn, 'click')
+   .then(e => console.log('event click happens', e)).then(e => (btn.disabled = true, e))
+