Mitrofanova Natali 3 rokov pred
rodič
commit
9a41b7feca

+ 23 - 0
HW 14 Async Await/index.html

@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Document</title>
+    <link rel="stylesheet" href="style.css">
+</head>
+<body>
+
+
+    <div id="wrapper"> 
+        <!-- <div id="red"></div>
+        <div id="yellow"></div>
+        <div id="green"></div> -->
+    </div>
+
+    <button id="test">test</button> 
+    <div id="wrapper2"></div>
+    <button id="go">GO</button> 
+
+    <script src="script.js"></script>
+</body>
+</html>

+ 151 - 0
HW 14 Async Await/script.js

@@ -0,0 +1,151 @@
+const red = document.getElementById("red");
+const yellow = document.getElementById("yellow");
+const green = document.getElementById("green");
+        
+const knopka = document.getElementById("test");
+const btnGo = document.getElementById("go")
+const wrapper = document.getElementById("wrapper");
+const wrapper2 = document.getElementById("wrapper2")
+//  Stage1 Светофор
+// const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
+
+// async function trafficLight(){
+//     while (true){
+//       red.classList.remove("red")
+//       green.classList.add("green");                    // включаем зеленый
+//         await delay(5000)                        
+//         yellow.classList.add("yellow")                 // включаем желтый  
+//         green.classList.remove("green")
+        
+//         await delay(2000)                           
+//         red.classList.add("red")                    // включаем красный
+//         yellow.classList.remove("yellow")
+        
+//         await delay(5000)
+//     }
+    
+// }
+
+// trafficLight()
+
+// stage 2 
+const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
+
+async function trafficLight2(elem, redTime=3000, yellowTime=1000, greenTime=3000){
+  
+  for(let i=0; i<3; i++){
+        elem.innerHTML +=`<div></div>`;
+      }
+
+      while(true){
+        elem.children[0].style.backgroundColor="red";
+        elem.children[2].style.backgroundColor="";
+        await delay(redTime);
+        elem.children[1].style.backgroundColor="yellow";
+        elem.children[0].style.backgroundColor="";
+        await delay(yellowTime);
+        elem.children[2].style.backgroundColor="green";
+        elem.children[1].style.backgroundColor="";
+        await delay(greenTime);
+      }
+}
+trafficLight2(wrapper,3000,1000,3000);
+
+//  domEventPromise
+function domEventPromise(element, eventName){
+  return new Promise((resolve) => {
+    const eventHandler = (event) => {
+      resolve(event.target);
+      element.removeEventListener(eventName,eventHandler)
+    }
+    element.addEventListener(eventName, eventHandler);
+  })
+}
+domEventPromise(knopka, 'click').then(e => console.log('event click happens', e))
+
+
+
+//  PedestrianTrafficLight
+
+// async function pedestrianLight(elem, redTime =3000, greenTime=3000){
+
+//   for(let i=0; i<2; i++){
+//     elem.innerHTML +=`<div></div>`;
+//   }
+
+//       while(true){
+//         elem.children[0].style.backgroundColor="red";
+//         elem.children[1].style.backgroundColor="";
+//         await Promise.race([delay(redTime), domEventPromise(btnGo, 'click')]);
+
+//         elem.children[1].style.backgroundColor="green";
+//         elem.children[0].style.backgroundColor="";
+//         await delay(greenTime);
+//       }
+// }
+// pedestrianLight(wrapper2, 5000,2000)
+
+
+// stage 3
+async function pedestrianLight(elem, redTime =3000, greenTime=3000){
+
+  for(let i=0; i<2; i++){
+    elem.innerHTML +=`<div></div>`;
+  }
+
+      while(true){
+        elem.children[0].style.backgroundColor="red";
+        elem.children[1].style.backgroundColor="";
+        await Promise.race([delay(redTime), domEventPromise(btnGo, 'click')]);
+
+        elem.children[1].style.backgroundColor="green";
+        elem.children[0].style.backgroundColor="";
+        await delay(greenTime);
+      }
+}
+pedestrianLight(wrapper2, 5000,3000)
+btnGo.addEventListener('click', handler)
+
+async function handler(){
+  btnGo.disabled = true;
+  await delay(6000)
+  btnGo.disabled = false;
+  domEventPromise(knopka, 'click')
+}
+
+// speedtest
+
+async function speedtest(getPromise, count,parallel=1){
+    const startTime = performance.now();
+  
+    for (let i = 0; i < count; i++){
+      const arrPromise = [];
+      await ( async ()=>{
+          for (j=0; j<parallel; j++){
+            arrPromise.push(getPromise())
+          }
+          await Promise.all(arrPromise);
+      })()
+    }
+  
+    const finishTime = performance.now();
+
+    let duration = finishTime - startTime;
+    
+      return {
+        duration,
+          querySpeed: count / duration, //средняя скорость одного запроса
+          queryDuration: duration/count,
+          parallelSpeed : (parallel*count)/duration,
+          parallelDuration: duration/(count * parallel)
+      }
+  }
+  
+  speedtest(() => delay(1000).then(result => console.log(result)), 10, 10 ).then(res=>console.log(res))
+  // {duration: 10000, 
+  // querySpeed: 0.001, //1 тысячная запроса за миллисекунду
+  // queryDuration: 1000, //1000 миллисекунд на один реальный запрос в среднем 
+  // parallelSpeed: 0.01  // 100 запросов за 10000 миллисекунд
+  // parallelDuration: 100 // 100 запросов за 10000 миллисекунд
+  speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5).then(res=>console.log(res))
+

+ 50 - 0
HW 14 Async Await/style.css

@@ -0,0 +1,50 @@
+body{
+    display: flex;
+
+}
+
+#wrapper, #wrapper2{
+    width: 200px;
+    height: 600px;
+    background-color: #796e6e;
+    border-radius: 35px;
+    display: flex;
+    flex-direction: column;
+    justify-content: center;
+    align-items: center;
+    margin: 0 50px;
+    
+}
+#wrapper div, #wrapper2 div{
+    width: 150px;
+    height: 150px;
+    background-color: grey;
+    border-radius: 50%;
+    margin-top: 20px;
+    
+}
+
+button{
+    height: 100px;
+    width: 100px;
+    font-size: 50px;
+    border-radius: 25%;
+    background-color: pink;
+}
+/* .wrapper2{
+    height: 400px;
+}
+.red{
+    background-color: red;
+}
+.yellow{
+    background-color: orange;
+}
+.green{
+    background-color: green;
+} */
+
+#wrapper2{
+    height: 400px;
+}
+