script.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  2. let div = document.createElement('div')
  3. document.body.append(div)
  4. //domEventPromise
  5. let btn = document.createElement('button')
  6. btn.innerText = 'click'
  7. document.body.append(btn)
  8. function domEventPromise(elem, eventName) {
  9. let prom = new Promise((resolve, reject) => {
  10. function doAndRemove(event) {
  11. elem.removeEventListener(eventName, doAndRemove)
  12. resolve(event)
  13. }
  14. elem.addEventListener(eventName, doAndRemove)
  15. })
  16. return prom
  17. }
  18. domEventPromise(btn, 'click').then((event) => console.log('event click happens', event))
  19. async function pedestrianTrafficLight(parent, redTime = 4000, yellowTime = 2000, greenTime = 2000) {
  20. let container = document.createElement('div')
  21. container.style.backgroundColor = 'grey'
  22. container.style.width = 'fit-content'
  23. container.style.padding = '5px'
  24. let btn = document.createElement('button')
  25. btn.innerText = 'pedestrian button'
  26. let colors = []
  27. for(let i = 0; i < 3; i++) {
  28. let color = document.createElement('div')
  29. color.style.backgroundColor = 'black'
  30. color.style.width = '100px'
  31. color.style.height = '100px'
  32. color.style.borderRadius = '100%'
  33. color.style.margin = '10px'
  34. container.append(color)
  35. colors.push(color)
  36. }
  37. container.append(btn)
  38. parent.append(container)
  39. while (true) {
  40. let pressed = false
  41. btn.disabled = false
  42. switch ('red') {
  43. case 'red':
  44. colors[0].style.backgroundColor = 'red'
  45. colors[0].style.boxShadow = '0px 2px 10px red'
  46. await Promise.race([delay(redTime), domEventPromise(btn, 'click')]).then((e) => {
  47. if (typeof e === 'object') {
  48. pressed = true
  49. }
  50. })
  51. colors[0].style.boxShadow = 'none'
  52. colors[0].style.backgroundColor = 'black'
  53. case 'yellow':
  54. if (pressed === true) break;
  55. colors[1].style.backgroundColor = 'yellow'
  56. colors[1].style.boxShadow = '0px 2px 10px yellow'
  57. await Promise.race([delay(yellowTime), domEventPromise(btn, 'click')]).then((e) => {
  58. if (typeof e === 'object') {
  59. pressed = true
  60. }
  61. })
  62. colors[1].style.boxShadow = 'none'
  63. colors[1].style.backgroundColor = 'black'
  64. }
  65. colors[2].style.backgroundColor = 'lightgreen'
  66. colors[2].style.boxShadow = '0px 2px 10px lightgreen'
  67. btn.disabled = true
  68. await delay(greenTime)
  69. colors[2].style.boxShadow = 'none'
  70. colors[2].style.backgroundColor = 'black'
  71. }
  72. }
  73. pedestrianTrafficLight(div, 3000, 3000, 3000)
  74. // speedtest
  75. async function speedtest(getPromise, count,parallel=1){
  76. let _parallel = parallel
  77. let duration = performance.now()
  78. for(let i = 0; i < count; i++) {
  79. let promises = []
  80. while(parallel !== 0) {
  81. promises.push(await getPromise())
  82. parallel -= 1
  83. }
  84. Promise.all(promises)
  85. }
  86. duration = performance.now() - duration
  87. return {
  88. duration: duration,
  89. querySpeed: count / duration,
  90. queryDuration: duration / count,
  91. parallelSpeed: (count * _parallel) / (duration / count),
  92. parallelDuration: duration / (count * _parallel)
  93. }
  94. }
  95. speedtest(() => delay(1000), 10, 10 ).then(result => console.log('result',result))
  96. speedtest(() => fetch('http://swapi.dev/api/people/1').then(res => res.json()), 10, 5).then(result => console.log('result',result))