index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  2. async function trafficLight() {
  3. let red = document.getElementById('red')
  4. let yellow = document.getElementById('yellow')
  5. let green = document.getElementById('green')
  6. let redTimer = document.getElementById('redTimer')
  7. let greenTimer = document.getElementById('greenTimer')
  8. while (true) {
  9. await delay(500)
  10. .then(() => red.style.opacity = 1)
  11. await delay(10000)
  12. .then(() => red.style.opacity = 0.3)
  13. .then(timer(10, redTimer))
  14. await delay(500)
  15. .then(() => yellow.style.opacity = 1)
  16. await delay(1000)
  17. .then(() => yellow.style.opacity = 0.3)
  18. await delay(500)
  19. .then(() => green.style.opacity = 1)
  20. await delay(10000)
  21. .then(() => green.style.opacity = 0.3)
  22. .then(timer(10, greenTimer))
  23. await delay(500)
  24. .then(() => yellow.style.opacity = 1)
  25. await delay(1000)
  26. .then(() => yellow.style.opacity = 0.3)
  27. }
  28. }
  29. let btn = document.getElementById('btn')
  30. async function humanTrafficLightGreen() {
  31. let green = document.getElementById('humanGreen')
  32. let greenTimer = document.getElementById('humanGreenTimer')
  33. while (true) {
  34. await delay(500)
  35. .then(() => green.style.opacity = 1)
  36. await delay(10000)
  37. .then(() => green.style.opacity = 0.3)
  38. .then(timer(10, greenTimer))
  39. await delay(13500)
  40. }
  41. }
  42. async function humanTrafficLightRed() {
  43. let red = document.getElementById('humanRed')
  44. let redTimer = document.getElementById('humanRedTimer')
  45. while (true) {
  46. await delay(12500)
  47. .then(() => red.style.opacity = 1)
  48. await delay(10000)
  49. .then(() => red.style.opacity = 0.3)
  50. .then(timer(10, redTimer))
  51. await delay(1500)
  52. }
  53. }
  54. async function timer(time, value) {
  55. for (time; time > 0; time--) {
  56. value.innerHTML = time
  57. await delay(1000)
  58. value.innerHTML = ""
  59. }
  60. }
  61. trafficLight()
  62. async function humanTrafficLight() {
  63. while (true) {
  64. await Promise.race([humanTrafficLightGreen(),
  65. humanTrafficLightRed(), domEventPromise(btn, 'click').then(e => {
  66. console.log('event click happens', e)
  67. humanTrafficLight()
  68. })])
  69. }
  70. }
  71. humanTrafficLight()
  72. function domEventPromise(click, eventName) {
  73. return new Promise(function (resolve) {
  74. // debugger
  75. click.addEventListener(eventName, function clickListener(e) {
  76. click.removeEventListener(eventName, clickListener);
  77. resolve(e);
  78. });
  79. });
  80. }
  81. async function speedtest(getPromise, count,parallel=1){
  82. let duration = performance.now();
  83. let request = parallel;
  84. let arr = [];
  85. for (let i = 0; i < count; i++) {
  86. arr[i] = getPromise();
  87. parallel -= 1;
  88. await Promise.all(arr);
  89. }
  90. duration = performance.now() - duration;
  91. return {
  92. duration: duration,
  93. querySpeed: count / duration,
  94. queryDuration: duration / count,
  95. parallelSpeed: (count / duration) * request,
  96. parallelDuration: duration / (request * count)
  97. }
  98. }
  99. speedtest(() => delay(1000), 10, 10 ).then(result => console.log(result))
  100. speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5).then(result => console.log(result))