我需要觸發一次只有一個自定義事件,其功能類似於domready事件,因爲如果在事件發生後添加新事件,它們會立即觸發。使用自定義事件模擬domready事件(mootools)
這是一些代碼,無法執行,直到一定的數據和資源進行初始化,所以我想要做這樣的事情:
// I am including a script (loadResources.js) to load data and other resources,
// when loadResources.js is done doing it's thing it will fire resourcesAreLoaded with:
window.fireEvent('resourcesAreLoaded');
window.addEvent('resourcesAreLoaded', function() {
// this is fine
});
$('mybutton').addEvent('click', function() {
window.addEvent('resourcesAreLoaded', function() {
// this is not fine, because resourcesAreLoaded has already fired
// by the time the button is clicked
});
});
如果可能的話,我想resourcesAreLoaded
像domready
的功能,並執行代碼立即如果事件已經發射:
window.addEvent('testIsReady', function() {
alert('firing test');
});
window.fireEvent('testIsReady');
window.addEvent('test', function() {
// this will never execute unless I call fireEvent('testIsReady') again
alert('test 2');
});
window.addEvent('domready', function() {
alert('domready is firing');
});
window.addEvent('domready', function() {
setTimeout(function() {
alert('domready has already fired, so this is executed immediately');
}, 500);
});
+1,很好回答 – 2010-05-07 09:59:45
thanks @Dimitar – Anurag 2010-05-07 16:11:48