开发者

jQuery $.load() sends a GET request multiple times

开发者 https://www.devze.com 2023-03-27 07:53 出处:网络
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 sni

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号