script.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms))
  2. // async function trafficLight(parent, redTime=4000, yellowTime=2000, greenTime=2000){
  3. // let container = document.createElement('div')
  4. // container.style.backgroundColor = 'grey'
  5. // container.style.width = 'fit-content'
  6. // container.style.padding = '5px'
  7. // let red = document.createElement('div')
  8. // let yellow = document.createElement('div')
  9. // let green = document.createElement('div')
  10. // let elems = [red, yellow, green]
  11. // for(let item of elems) {
  12. // item.style.backgroundColor = 'black'
  13. // item.style.width = '100px'
  14. // item.style.height = '100px'
  15. // item.style.borderRadius = '100%'
  16. // item.style.margin = '10px'
  17. // container.append(item)
  18. // }
  19. // parent.append(container)
  20. // while (true){
  21. // red.style.backgroundColor = 'red'
  22. // red.style.boxShadow = '0px 2px 10px red'
  23. // await delay(redTime)
  24. // red.style.boxShadow = 'none'
  25. // red.style.backgroundColor = 'black'
  26. // yellow.style.backgroundColor = 'yellow'
  27. // yellow.style.boxShadow = '0px 2px 10px yellow'
  28. // await delay(yellowTime)
  29. // yellow.style.boxShadow = 'none'
  30. // yellow.style.backgroundColor = 'black'
  31. // green.style.backgroundColor = 'lightgreen'
  32. // green.style.boxShadow = '0px 2px 10px lightgreen'
  33. // await delay(greenTime)
  34. // green.style.boxShadow = 'none'
  35. // green.style.backgroundColor = 'black'
  36. // }
  37. // }
  38. let div = document.createElement('div')
  39. document.body.append(div)
  40. //trafficLight(div, 5000, 3000, 2000)
  41. //domEventPromise
  42. let btn = document.createElement('button')
  43. btn.innerText = 'click'
  44. document.body.append(btn)
  45. function domEventPromise(elem, eventName) {
  46. let prom = new Promise((resolve, reject) => {
  47. function doAndRemove(event) {
  48. elem.removeEventListener(eventName, doAndRemove)
  49. //console.log('clicked')
  50. resolve(event)
  51. }
  52. elem.addEventListener(eventName, doAndRemove)
  53. })
  54. return prom
  55. }
  56. domEventPromise(btn, 'click').then( (event) => console.log('event click happens', event))
  57. async function pedestrianTrafficLight (parent, redTime=4000, yellowTime=2000, greenTime=2000) {
  58. let container = document.createElement('div')
  59. container.style.backgroundColor = 'grey'
  60. container.style.width = 'fit-content'
  61. container.style.padding = '5px'
  62. let btn = document.createElement('button')
  63. btn.innerText = 'pedestrian button'
  64. let red = document.createElement('div')
  65. let yellow = document.createElement('div')
  66. let green = document.createElement('div')
  67. let elems = [red, yellow, green]
  68. for(let item of elems) {
  69. item.style.backgroundColor = 'black'
  70. item.style.width = '100px'
  71. item.style.height = '100px'
  72. item.style.borderRadius = '100%'
  73. item.style.margin = '10px'
  74. container.append(item)
  75. }
  76. container.append(btn)
  77. parent.append(container)
  78. while (true){
  79. let buttonPresed = domEventPromise(btn, 'click')
  80. Promise.race([delay, buttonPresed]).then(e => console.log(e))
  81. red.style.backgroundColor = 'red'
  82. red.style.boxShadow = '0px 2px 10px red'
  83. await delay(redTime)
  84. Promise.race([delay, buttonPresed]).then(e => console.log(e))
  85. red.style.boxShadow = 'none'
  86. red.style.backgroundColor = 'black'
  87. yellow.style.backgroundColor = 'yellow'
  88. yellow.style.boxShadow = '0px 2px 10px yellow'
  89. Promise.race([delay, buttonPresed]).then(e => console.log(e))
  90. await delay(yellowTime)
  91. yellow.style.boxShadow = 'none'
  92. yellow.style.backgroundColor = 'black'
  93. green.style.backgroundColor = 'lightgreen'
  94. green.style.boxShadow = '0px 2px 10px lightgreen'
  95. Promise.race([delay, buttonPresed]).then(e => console.log(e))
  96. await delay(greenTime)
  97. green.style.boxShadow = 'none'
  98. green.style.backgroundColor = 'black'
  99. }
  100. }
  101. pedestrianTrafficLight(div, 3000, 3000, 3000)