我需要触发一次只有一个自定义事件,其功能类似于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