pul8219 / TIL

Today I Learned๐Ÿ“’
2 stars 0 forks source link

FE Study) STEP 21 ๋ฆฌ๋ทฐ #17

Open eyabc opened 3 years ago

eyabc commented 3 years ago

Event ์˜ composed ์˜ต์…˜์— ๋Œ€ํ•œ ๋ถ€๋ถ„์„ ๋ณด๊ณ  ๊ถ๊ธˆํ•˜์—ฌ ์ฐพ์•„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ํ•ต์‹ฌ์€ shadow dom ์ด๋ผ๋Š” ๊ฐœ๋…์ธ๋ฐ https://wit.nts-corp.com/2019/03/27/5552 ์—ฌ๊ธฐ์— ๊ฐœ๋…์ด ์ž˜ ๋‚˜์™€ ์žˆ๋„ค์š”, ์‚ฌ์ดํŠธ๋“ค์— ํฌ๋กฌ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋ฅผ ์—ด์–ด์„œ element ๋ฅผ ๊ด€์ฐฐ์„ ํ–ˆ์„ ๋•Œ ์ข…์ข… ๋ณด์ด๋Š” ๊ฒƒ๋“ค์ด๋ผ ์ต์ˆ™ํ•˜๊ฒŒ ๋ณด์•˜๋„ค์š”

์ด๋ฒคํŠธ๋ฅผ ์ œ์–ดํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์„ ์˜๋ฏธ) ๋˜ํ•œ HTMLElement.click()์™€ ๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋„ ์žˆ๊ตฐ์š”!

โ— TODO ์ง€์—ฐ ์‹œ๊ฐ„์ด 0์ธ setTimeout์œผ๋กœ ๊ฐ์‹ธ๋ฉด ์ค‘์ฒฉ์ด๋ฒคํŠธ๊ฐ€ ์ฆ‰์‹œ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š๋Š” ์ด์œ ๋Š”?.. setTimeout ์˜ ์ง€์—ฐ์‹œ๊ฐ„์ด 0 ์ด๋ผ๊ณ  ํ•˜๋”๋ผ๋„, ๋น„๋™๊ธฐ ํ•จ์ˆ˜์ด๋ฉฐ, ํ์— ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ์ฆ‰์‹œ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  0์€ ์ตœ์†Œ ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์ž‘์—…๋“ค์— ๋ฐ€๋ ค ๋” ์ง€์—ฐ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ! https://evan-moon.github.io/2019/08/01/nodejs-event-loop-workflow/

eyabc commented 3 years ago
<pre id="rabbit">
  |\   /|
   \|_|/
   /. .\
  =\_Y_/=
   {>o<}
</pre>
<button onclick="hide()">hide()๋ฅผ ํ˜ธ์ถœํ•ด ํ† ๋ผ ์ˆจ๊ธฐ๊ธฐ</button> (1) onclick ์œผ๋กœ ์ธํ•ด hide() ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ์‹คํ–‰์ด๋จ

<script>
  // hide() will be called automatically in 2 seconds
  function hide() { (2) ์‹คํ–‰ 2 
    let event = new CustomEvent("hide", {
      cancelable: true // cancelable๋ฅผ true๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด preventDefault๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    }); (3) hide ๋ผ๋Š” ์ปค์Šคํ…€ ์ด๋ฒคํŠธ ์ƒ์„ฑ
    if (!rabbit.dispatchEvent(event)) { (4) ์ปค์Šคํ…€ ์ด๋ฒคํŠธ ํ˜ธ์ถœ 
      alert('๊ธฐ๋ณธ ๋™์ž‘์ด ํ•ธ๋“ค๋Ÿฌ์— ์˜ํ•ด ์ทจ์†Œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.'); (7) confirm ์ด false ์ผ ๊ฒฝ์šฐ (์ด๋ฒคํŠธ์˜ ๊ธฐ๋ณธ๋™์ž‘์ด ์ทจ์†Œ๋  ๊ฒฝ์šฐ)
    } else {
      rabbit.hidden = true; (8) confirm ์ด true ์ผ ๊ฒฝ์šฐ (์ด๋ฒคํŠธ์˜ ๊ธฐ๋ณธ๋™์ž‘์ด ์ทจ์†Œ๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ) 
    }
  }

  rabbit.addEventListener('hide', function(event) { (5) ๋ฐ”์ธ๋”ฉ๋œ hide ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์‹คํ–‰ 
    if (confirm("preventDefault๋ฅผ ํ˜ธ์ถœํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?")) { (6) ์ด๋ฒคํŠธ์˜ ๊ธฐ๋ณธ๋™์ž‘์ด ์ทจ์†Œ๋˜๋ฉด elem.dispatchEvent(event) ๊ฐ€ false ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ confirm yes ๋ฅผ ํ•  ๊ฒฝ์šฐ, rabbit.dispatchEvent(event) ๋Š”, ๊ธฐ๋ณธ ์ด๋ฒคํŠธ ๋™์ž‘์ด ์ทจ์†Œ๋˜์—ˆ์œผ๋ฏ€๋กœ false ๋ฐ˜ํ™˜, confirm no ์ผ ๊ฒฝ์šฐ event.preventDefault(); ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ true ๋ฐ˜ํ™˜ 
      event.preventDefault(); 
    }
  });
</script>

โ— TODO ์•„๋ž˜ ์˜ˆ์ œ์—์„œ ๊ธฐ๋ณธ๋™์ž‘์˜ ์—ญํ• ์„ ํ•˜๋Š” ๊ฑด ํ† ๋ผ๋ฅผ ์ˆจ๊ธฐ๋Š” else{rabbit.hidden=true;} ๋ถ€๋ถ„์„ ์˜๋ฏธํ•˜๋Š”๊ฑด๊ฐ€?

pul8219 commented 3 years ago

@eyabc composed ์˜ต์…˜๋„ setTimeout ๊ด€๋ จ ๋‚ด์šฉ๋„ ์งš์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋ฌธ์„œ ์ฐฌ์ฐฌํžˆ ์ฝ์–ด๋ณผ๊ฒŒ์š” ํ† ๋ผ(?) ์ฝ”๋“œ๋Š” ์€์˜๋‹˜์ด ์˜ฌ๋ ค์ฃผ์‹  ์ฝ”๋“œ๋ž‘ ํ˜•์šฑ๋‹˜ ๋ฌธ์„œ๋ณด๊ณ  ๋” ๋ช…ํ™•ํžˆ ์ดํ•ด๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ https://ko.javascript.info/dispatch-events ๋ฌธ์„œ์— ์žˆ๋˜ ์ปค์Šคํ…€ ์ด๋ฒคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ๋””์ŠคํŒจ์นญ ํ•ด ์ฃผ๋Š” ์ฝ”๋“œ์— ์›ํ•˜๋Š” ๋™์ž‘์„ ๋„ฃ์œผ๋ฉด, ์ปค์Šคํ…€ ์ด๋ฒคํŠธ์—๋„ ๊ธฐ๋ณธ ๋™์ž‘์„ ์„ค์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด... ์ด ์˜ˆ์ œ์—์„  ํ† ๋ผ๋ฅผ ์ˆจ๊ธฐ๋Š” ๋™์ž‘์ด ๊ธฐ๋ณธ๋™์ž‘์ด์—ˆ๋˜ ๊ฑฐ๊ณ  ๊ธฐ๋ณธ ๋™์ž‘์„ ์ทจ์†Œํ•˜๋Š๋ƒ ๋งˆ๋ƒ์— ๋”ฐ๋ผ if else๋กœ ํ๋ฅผ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๊ฑฐ๊ฒ ๊ตฐ์š” ์ปค์Šคํ…€ ์ด๋ฒคํŠธ์— ๊ธฐ๋ณธ๋™์ž‘์„ ์„ค์ •ํ•  ๋•Œ ๋ณดํ†ต ์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š”์ง€ ๊ถ๊ธˆํ•˜์—ฌ ์ƒ๊ฒผ๋˜ ์˜๋ฌธ์ ์ž…๋‹ˆ๋‹ค! ์ฝ”๋“œ ๊ธฐ์ž…ํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ด์š”

khw970421 commented 3 years ago

CustomEvent๊ฐ€ newEvent์™€ ๋‹ค๋ฅธ ์ ๋ถ€๋ถ„์„ ๋” ๋ฐฐ์šฐ๋ฉฐ ์ž˜ ์ดํ•ดํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ์ œ๊ฐ€ ๊ถ๊ธˆ์ฆ์ด ์žˆ๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด ๊ณ ๋ฏผํ•ด์ฃผ๋ฉฐ ๋‹ต์„ ํ•ด์ฃผ์‹œ๋Š” ๋ถ€๋ถ„ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.