12345678910111213141516171819202122232425262728293031323334 |
- <header>domEventPromise</header>
- <body>
- <button id="knopka">test button</button>
- <script>
- function domEventPromise(element, eventName) {
- function executor(resolve) {
- function myOnClick(event) {
- element.removeEventListener(eventName, myOnClick);
- resolve(event);
- }
- element.addEventListener(eventName, myOnClick);
- }
- return new Promise(executor);
- }
- /*function domEventPromise1(element, eventName) {
- return
- new Promise(
- function (resolveFunc) {
- function executor(event) {
- element.removeEventListener(eventName, executor);
- resolveFunc(event);
- //happy hacking
- }
- element.addEventListener(eventName, executor);
- }
- )
- }*/
- domEventPromise(knopka, 'click').then(e => console.log('event click happens', e));
- </script>
- </body>
|