const one = (element, events, handler) => {
let executed = false;
let eventsArray = [];
if (/array/i.test(Object.prototype.toString.call(events))) {
eventsArray = events;
} else {
eventsArray.push(events);
}
// 事件触发时执行,执行过程中解绑,并改变标记变量executed的状态
const onceHanler = e => {
if (executed) {
return;
}
eventsArray.forEach(eventName => element.removeEventListener(eventName, onceHanler, false));
handler(e);
executed = true;
}
eventsArray.forEach(eventName => element.addEventListener(eventName, onceHanler, false));
}
one(element, ['animationend', 'webkitAnimationEnd', 'mozAnimationEnd'], handler);
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。