Saturday, February 20, 2010

I spent a lot of time trying to dynamically load scripts. The first problem is that you can't run functions until the scripts are loaded. That is pretty easy to solve. I built my own little framework for testing whether the script was loaded. Then I ran into another problem. If the script wasn't loaded I needed to set a timer and try again later. Well the timer in JavaScript is a little funky in that you can't pass parameters to it easily. After much trial and tribulation (and reading) I came up with this:



function mytimeout(w,t) {
var self = this;
self.what = function(c){alert(w);};
self.mytimer = function() {setTimeout(function(){self.what(w);},t);};

}
function Initialize() {
//Load Scripts

alert('loading');
var t1 = new mytimeout('bob',3000);
var t2 = new mytimeout('carol',1000);
t1.mytimer();
t2.mytimer();

}



This works nicely on Firefox for Max and IE for Windows. This of course is the generic version. The Initialize gets called in the index.html and this is in a loaded JavaScript file. I hope someone else might find this useful. I think it is pretty easy to understand. To give fair credit I did work off the reading from the website:

http://www.west-wind.com/weblog/posts/5033.aspx

No comments:

Post a Comment