onclick 이벤트 중복 방지

GisangLee·2022년 3월 23일
0

javascript

목록 보기
1/1

이벤트가 중복으로 발생하는 경우는 아주 빈번하게 일어난다.

이럴 때 해결 하는 방법은 이벤트를 unbind 후 bind하는 것이다.

//클릭이벤트
$("#test-button2").unbind("click");

//클릭이벤트
$("#test-button2").bind("click",function(){ alert("click event"); });

// or
//클릭이벤트
$("#test-button2").unbind("click").bind("click",function(){ alert("click event"); });

마지막으로 설정한 이벤트를 제외하고 마지막에 정의된 것만 실행하려면
off를 사용해도 되지만 이벤트를 unbind한 후 bind를 해 준다.

profile
포폴 및 이력서 : https://gisanglee.github.io/web-porfolio/

0개의 댓글