Best pre-onload
-
Comments:
- here.
I’ve been using domFunction() to determine whether it’s safe to do all of my DOM modification code, but with some testing, I noticed that some pages were causing Firefox to eat up huge amounts of CPU time. I tracked the error down to the domFunction call, as it uses setInterval() to run a function every n seconds. This in itself is nice to know (I was looking for a similar feature a couple of days ago), but the nice part is that it has a stopping function: clearInterval(). You are supposed to use it like this:
1 timer = setInterval("Function();", interval);
This will then call the function Function every interval milliseconds. Neato. Except I only want a particular thing to happen once. So, as a part of my code, I was calling clearInterval(timer) inside a function called by Function.
1 function Startup(){
2 Gravatars();
3 .
4 .
5 .
6 HumanTimePosts();
7
8 clearInterval(timer);
9 }
10
11 //Code for all functions in here.
12
13 timer = setInterval(function(){
14 if (document.getElementById(\"footer\")){
15 Startup();
16 }
17 }, 1000);
Which should work. Except for some reason, calls to clearInterval cannot come from inside functions called by setInterval. Or something like that. My next solution was to use:
1 function checkDOM(){
2 if (getById("footer")||getById("credit")) Startup();
3 else setTimeout("checkDOM();",100);
4 }
5 setTimeout("checkDOM();"
Which solves the problem, but it ugly. Finally, I came across this little number:
1 if (document.addEventListener) {
2 document.addEventListener("DOMContentLoaded", Startup, null);
3 }
This only works with Mozilla browsers, so for the time being I’ve combined the last two.