而不是在JS中嵌套回調,我想觸發並聆聽我自己的自定義事件。我不需要或不想訪問DOM。這裏有一個例子:當我的函數完成時用javascript(或jquery)觸發自定義事件
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代碼或像jQuery一樣的庫嗎?如果不是,避免嵌套回調的好方法是什麼?
謝謝!