2 title: Javascript delaying
8 In need of executing Javascript after the page has loaded, or something else
9 has been initialized I came up with a simple but useful tiny "Delayer":
13 * @param handler a callback accepting Delayer.dispatch as argument
15 function Delayer(handler) {
18 this.dispatch = function() {
19 for (var i = 0; i < self.length; ++i) {
20 console.log("running delayed init "+i+": "+self[i]);
25 if (typeof handler == "function" || handler instanceof Function) {
26 handler(this.dispatch);
30 Delayer.prototype = Array.prototype;
33 It can be initialized the following way:
35 /* e.g. with jQuery */
36 window.delayedInits = new Delayer($(document).ready);
37 /* e.g. with Facebook */
38 window.fbDelayedInits = new Delayer(function(dp){window.fbAsyncInit = dp;});
41 Then you push your work the usual way:
43 delayedInits.push(function() {alert("Hello, delayed!");});