jQuery $.load() sends a GET request multiple times
There's some code that looks like this:
$(window).load(function(){
sendAGetRequest();
}
Periodically, on production, the GET request is being sent multiple times. I can see this using a packet sniffer.
This isn't reproducible locally.
There's a couple of similar questions on here but they don't seem to be much use.
What I'd like to know is:
- Why does the function sendAGetRequest() get called multiple times?
- What sort of things could cause this to only happen on production and not local?
I suspect the problem to be $(window).load
and am considering changing this to $(documen开发者_JAVA百科t).ready
. Is that a good call? As I said, I can't reproduce the problem locally and I wouldn't want to upload something to live without knowing whether it might actually fix things or not.
$(window).load will only be triggered when page is fully loaded including pictures. While $(document).ready will be triggerred when the whole DOM is ready. This is a much better point to trigger such request as you don't have to wait for the whole resources to be loaded. i am not sure why the function is called multiple times, although in theory it shouldn't.
I would imagine that the event that the load()
function that attaches the event handler is getting called multiple times. Every time you call load()
, another function handler will get attached, and all of those handlers will get called when the load event fires.
Using document.ready
is a good call, but won't help you if the above is true.
精彩评论