hw_17_03_domEventPromise.html 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. <header>domEventPromise</header>
  2. <body>
  3. <button id="knopka">test button</button>
  4. <script>
  5. function domEventPromise(element, eventName) {
  6. function executor(resolve) {
  7. function myOnClick(event) {
  8. element.removeEventListener(eventName, myOnClick);
  9. resolve(event);
  10. }
  11. element.addEventListener(eventName, myOnClick);
  12. }
  13. return new Promise(executor);
  14. }
  15. /*function domEventPromise1(element, eventName) {
  16. return
  17. new Promise(
  18. function (resolveFunc) {
  19. function executor(event) {
  20. element.removeEventListener(eventName, executor);
  21. resolveFunc(event);
  22. //happy hacking
  23. }
  24. element.addEventListener(eventName, executor);
  25. }
  26. )
  27. }*/
  28. domEventPromise(knopka, 'click').then(e => console.log('event click happens', e));
  29. </script>
  30. </body>