JS에 콜백을 중첩하는 대신 사용자 정의 이벤트를 실행하고 수신 대기하고 싶습니다. DOM이 필요하지 않거나 액세스하고 싶지 않습니다. 다음은 예입니다.
function doSomething(){
//...
$.trigger('finished-doSomething'); //fire the event 'finished-doSomething'
}
//when the event 'finished-doSomething' is fired -> execute the function in the second param
$.live('finished-doSomething', function(){
alert("I finished-doSomething");
});
일반 JavaScript code나 jQuery와 같은 라이브러리로 그렇게 할 수 있습니까? 그렇지 않다면 중첩된 콜백을 피하기 위한 좋은 접근 방식은 무엇입니까?
출처: http://api.jquery.com/trigger/