It does not look like your code is complete in your question but I'm going to hypothesize the following. What you describe looks like a race condition:
When the cache does not contain your data, the assignment to window.onload
happens before the onload
even is fired, so your handler is called.
When the cache already contains your data, then the onload
event is fired before the assignment to window.onload
happens, so your handler is never called.
I would suggest using the domReady
plugin because it is designed to handle both cases. I've never used it myself but from what I can gather from the documentation, something like:
require(['domReady!', 'game','comm','misc','soundutil','sprite','gui','constants'],...
should work. The exclamation mark is not a typo, by the way. You then need to make your onload
handler be the body of the function you pass to require
.
Note that document.onload
suffers from the same problem. It is even more prone to the problem you're having because it often fires before window.onload
does.
ETA: As Shrike says, you can also use the jQuery method of waiting: $(document).ready(...)
. My rule of thumb here is if I'm already using jQuery (which I actually do in all my current projects), then I'd use $(document).ready(...)
, otherwise I'd use domReady
. The difference between the two methods has been examined in detail in this question.