These will be your internal or private variables. Finally, I take the three functions that I want to
be available publicly and return them in an object literal.
With closures, those public functions still have access to the internal variables. The other
thing that had to change was how I referred to those internal variables??”I dropped the this
keyword from them.
In the future, you can create a new version of the ElementReady script, and as long as the
interface for those three functions hasn??™t changed, it doesn??™t matter what you??™ve done with the
rest of the implementation.
Here??™s the final version with the changes implemented:
var ElementReady= new function(){
var polled = []; /* store polled elements */
var timer = null; /* store timer */
var timerStarted = false;
CHAPTER 3 n OBJECT-ORIENTED PROGRAMMING 72
var ceasePoll = function()
{
clearTimeout(timer);
timerStarted = false;
};
var startPoll = function()
{
if(!timerStarted) {
timer = setTimeout( function(){ElementReady.check(false)}, 100);
}
};
return {
check:function(clean)
{
for(var i=0;i
{
if(document.getElementById(polled[i]['element']))
{
polled[i]['callback']();
polled.splice(i--,1);
}else if(clean){
polled.splice(i--,1);
}
}
if(polled.length == 0) ceasePoll();
},
cleanUp:function()
{
check(true);
ceasePoll();
},
chkDomId:function(elId,callback) {
var el = document.
Pages:
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131