你不能添加一個eventListener給任何函數,但是你可以在你的函數內觸發一個事件,並且使用你的函數作爲事件的回調函數。事件偵聽器用於處理事件。在你的情況下,如果你想添加事件到函數,你可以簡單地在Titanium中創建自定義事件。例如,如果您想在調用某個函數時執行某些特定操作,則可以簡單地按照以下方式進行操作。
//Creating the custom event
window.addEventListener('myEvent', function(){
alert('function called')
});
function foo(){
//Some actions
window.fireEvent('myEvent');
}
您還可以將事件添加到應用程序本身(應用程序級事件)。應用程序級別的事件對您的應用程序是全球性它們可以在所有上下文,功能範圍,CommonJS模塊等中訪問。你可以通過Ti.App模塊解僱他們並聽他們。
Ti.App.addEventListener('myAppEvent', function(){
alert('Application level event get fired');
});
//Fire the event like
Ti.App.fireEvent('myAppEvent');
請參考Event handling in Titanium更多細節