直接上代碼javascript
const target = document.getElementById('target');
// 當‘點擊’的時候 作某些事情
target.addEventListener('click', function(e) {
// dosomething(e)
});
const eventPromise = function(target, type) {
return new Promise((res) => {
target.addEventListener(type, function f(e) {
res(e);
target.removeEventListener(type, f);
});
});
};
// 無限期 等待 ’點擊‘ 的時候 作某些事情
(async function() {
while (true) {
const event = await eventPromise(target, 'click');
// dosomething(event)
}
})();
複製代碼