1
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);

clipboard.png


这波能反杀
12.6k 声望2.7k 粉丝