Browse Source

HW<14> 50%

Levshin95 1 year ago
parent
commit
76e84e4158
2 changed files with 138 additions and 0 deletions
  1. 44 0
      HW014/index.html
  2. 94 0
      HW014/index.js

+ 44 - 0
HW014/index.html

@@ -0,0 +1,44 @@
+<!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 class="allTraficLight" style="display: flex; justify-content:space-around">
+        <div class="carTraficLight">
+            <div class="topTraficLight" style="background-color: black; height: 50px; width: 150px; margin: 20px 0 0 75px; border-radius: 50% 50% 0 0;">
+            </div>
+            <div id="color" style="width: 200px; height: 500px; background-color: #000; display: flex; flex-direction: column; justify-content: space-around; align-items:center;margin-left: 50px">
+                <div id="red" style="background-color: red; width: 150px; height: 150px; border-radius: 50%; opacity: 0.3; display: flex; flex-direction: column; justify-content: space-around; align-items:center">
+                    <h1 id="redTimer" style="color: #000; opacity: 0.7; text-align: center; font-size: 100px;"></h1>
+                </div>
+                <div id="yellow" style="background-color: yellow; width: 150px; height: 150px; border-radius: 50%;  opacity: 0.3;"></div>
+                <div id="green" style="background-color: green; width: 150px; height: 150px; border-radius: 50%;  opacity: 0.3;display: flex; flex-direction: column; justify-content: space-around; align-items:center">
+                    <h1 id="greenTimer" style="color: #000; opacity: 0.7; text-align: center; font-size: 100px;"></h1>
+                </div>
+            </div>
+        </div>
+        <div class="humanTraficLight">
+            <div class="topTraficLight" style="background-color: black; height: 50px; width: 150px; margin: 20px 0 0 75px; border-radius: 50% 50% 0 0;">
+            </div>
+            <div id="humanColor" style="width: 200px; height: 350px; background-color: #000; display: flex; flex-direction: column; justify-content: space-around; align-items:center;margin-left: 50px">
+                <div id="humanRed" style="background-color: red; width: 150px; height: 150px; border-radius: 50%; opacity: 0.3; display: flex; flex-direction: column; justify-content: space-around; align-items:center">
+                    <h1 id="humanRedTimer" style="color: #000; opacity: 0.7; text-align: center; font-size: 100px;"></h1>
+                </div>
+                <div id="humanGreen" style="background-color: green; width: 150px; height: 150px; border-radius: 50%;  opacity: 0.3;display: flex; flex-direction: column; justify-content: space-around; align-items:center">
+                    <h1 id="humanGreenTimer" style="color: #000; opacity: 0.7; text-align: center; font-size: 100px;"></h1>
+                </div>
+            </div>
+
+            <button id="btn" style="margin: 20px 0 0 50px; width: 200px; height: 30px;">Переключить</button>
+        </div>
+    
+    </div>
+
+    
+    <script src="index.js"></script>
+</body>
+</html>

+ 94 - 0
HW014/index.js

@@ -0,0 +1,94 @@
+const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
+
+async function trafficLight(dom){
+    dom = document.getElementById('color')
+    let red = document.getElementById('red')
+    let yellow = document.getElementById('yellow')
+    let green = document.getElementById('green') 
+    let redTimer = document.getElementById('redTimer')
+    let greenTimer = document.getElementById('greenTimer')
+
+    while (true){
+        await delay(500)
+            .then(() => red.style.opacity = 1)
+
+        await delay(10000)
+            .then(() => red.style.opacity = 0.3)
+            .then(timer(10, redTimer))
+
+        await delay(500)
+            .then(() => yellow.style.opacity = 1)
+
+        await delay(1000)
+            .then(() => yellow.style.opacity = 0.3)
+
+        await delay(500)
+            .then(() => green.style.opacity = 1)
+
+        await delay(10000)
+            .then(() => green.style.opacity = 0.3)
+            .then(timer(10, greenTimer))
+
+        await delay(500)
+            .then(() => yellow.style.opacity = 1)
+
+        await delay(1000)
+            .then(() => yellow.style.opacity = 0.3)
+    }
+} 
+
+async function humanTrafficLight(dom){
+    dom = document.getElementById('humanColor')
+    let red = document.getElementById('humanRed')
+    let green = document.getElementById('humanGreen') 
+    let btn = document.getElementById('btn')
+    let redTimer = document.getElementById('humanRedTimer')
+    let greenTimer = document.getElementById('humanGreenTimer')
+
+    while (true){
+        await delay(500)
+            .then(() => green.style.opacity = 1)
+
+        await delay(10000)
+            .then(() => green.style.opacity = 0.3)
+            .then(timer(10, greenTimer))
+
+        await delay(1500)
+
+        await delay(500)
+            .then(() => red.style.opacity = 1)
+
+        await delay(10000)
+            .then(() => red.style.opacity = 0.3)
+            .then(timer(10, redTimer))
+
+        await delay(1500)
+
+    }
+} 
+
+async function timer(time, value) {
+    for (time; time> 0; time--){
+        value.innerHTML = time
+        await delay(1000)
+        value.innerHTML = ""
+    }
+}
+
+trafficLight()
+humanTrafficLight()
+
+function domEventPromise(click, eventName) {
+        return new Promise((resolve, reject) => {
+            click.addEventListener(eventName, (e) => {
+                resolve(e)
+            })
+            click.removeEventListener(eventName, (e) => {
+                resolve(e)
+            })
+        })
+    }
+btn.onclick = () => domEventPromise(btn, 'click').then( e => console.log('event click happens', e))
+
+
+