My question is somewhat similar to this one. We want to know if there's a way to determine if all javascript has completed (so no javascript is running). We have a lot of stuff that runs on a timeout after the page's onload event, so even after the page is loading, stuff could be happening for a few seconds. For a whole bunch of reasons, mostly relating to requirements from management, we need to know when all of these scripts have finished running. There's an arbitrary number of them so a general solution would be great.
+2
A:
The quickest and easiest was I can think of is to create a series of boolean values that tell you which processes have finished. After a method finishes, have it set one of the variables to true. When they are all true, you know that it's finished loading.
Kevin
2010-06-07 00:11:09
This is what I was thinking. It's going to get annoying to go through all those functions on all those pages to implement this, but oh well. C'Est la vie d'un programmeur!
Duracell
2010-06-07 00:20:00
@Sergio's solution is really simple and it allows you to avoid changing all your functions. Basically, any time you have a high-priority function you want to execute, run it through a special wrapper function (he called it executeWithNotify(), but you can call it anything you want. This function sets a public variable to `isRunning=true` when it starts and `isRunning=false` when it finished. All you have to do is run your high-priority scripts through this and change your low priority scripts to check for `isRunning`.
Andrew
2010-06-07 00:44:46
The problem with Sergio's function (although I like it) is that this assumes that the wrapper function knows when the other process has ended. It assumes that the process is synchronous when the other method returns. If your wrapped method were to call setTimeout etc., and results in the execution occurring out of order, the wrapper function would return the process had finished erroneously.
Kevin
2010-06-07 01:11:19
@Kevin, this was exactly the reason I asked the question in the first place. Otherwise it would be really simple to stick a javascript block right at the end of the file that flags the script as completed. But if you have a function which executes after a timeout then there's no guarantee that it will work.
Duracell
2010-06-21 03:01:55
A:
Maybe have those other scripts call an imDone() function when they complete.
Drew LeSueur
2010-06-07 00:11:11
+2
A:
You could create an excuteWithNotify(functionToEexcute) function. It will receive the function to execute as a parameter, execute it and then it will set the flags appropriately.
Sergio
2010-06-07 00:31:27
+1
A:
I would put all the JS files into one file. Then use the boolean method at the end.
Ray Kinsella
2010-06-07 00:42:42