Skip to content

Commit 07cc3ed

Browse files
author
Simon he
committed
chore: update
1 parent 2f1061b commit 07cc3ed

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/addEventListener.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
export function addEventListener(eventName: string, callback: (...args: any[]) => void, useCapture?: boolean, autoStop?: boolean): (() => void) {
2-
const remove = () => window.removeEventListener(eventName, callback)
3-
window.addEventListener(eventName, (e: Event) => {
1+
import { isStr } from './isStr'
2+
3+
export function addEventListener(target: Window | Document | Element | string, eventName: string, callback: (e: Event) => void, useCapture?: boolean, autoRemove?: boolean): (() => void) {
4+
if (isStr(target))
5+
target = document.querySelector(target as string) as Element
6+
const remove = () => (target as Element)?.removeEventListener(eventName, callback);
7+
(target as Element).addEventListener(eventName, (e: Event) => {
48
callback.call(e.target, e)
5-
if (autoStop)
9+
if (autoRemove)
610
remove()
711
}, useCapture)
812
return remove

0 commit comments

Comments
 (0)