title: Why do I get multiple XHR requests when network is slow?
body: <p>When I execute an action (upload a post request) on my website from my home network (Slow, less than 1Mb/s upload), the Chrome dev tool shows several XHR requests :</p>
  <p><a href="https://i.stack.imgur.com/xvuNI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xvuNI.png" alt="enter image description here" /></a></p>
  <p>While when I execute the same action but this time from my 4G phone network (Quicker, around 10Mb/s upload), the chrome dev tool shows only one (sometimes 2) XHR requests :</p>
  <p><a href="https://i.stack.imgur.com/XxplG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XxplG.png" alt="enter image description here" /></a></p>
  <p>How and why does it decides to split like that? Is it link with the fact that it is transfer-encoding chunked?</p>
  <p><a href="https://i.stack.imgur.com/7rGyX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7rGyX.png" alt="enter image description here" /></a></p>
  <p>or maybe because of the keepalive timeout of 5?</p>
  <p>The problem is that when there are those several XHR requests and that I reload the page before all the requests ends up to code 200 (which can take several minutes sometimes), I lose some data.</p>
  <p>Thanks.</p>
  <p>EDIT:</p>
  <p>As asked in the comment, here is the code I use for this part (I tried to clean it a bit by removing the parts not related to the problem, hope it helps)</p>
  <p>For exemple, if I update or add a &quot;tag&quot; to my note (my code is a note taking app &quot;Evernote&quot; type), the changed is trigered here in my index.php file :</p>
  <pre><code>&lt;div class=&quot;name_tags&quot;&gt;&lt;span&gt;&lt;input onfocus=&quot;updateidtags(this);&quot; id=&quot;tags'.$row['id'].'&quot; type=&quot;text&quot; placeholder=&quot;Tags ?&quot; value=&quot;'.$row['tags'].'&quot;&gt;&lt;/input&gt;&lt;/span&gt;&lt;/div&gt;
  </code></pre>
  <p>then the JavaScript file where all my functions are is called :</p>
  <pre><code>var editing = 0;
  var lastudpdate;
  var editingnote=-1;
  
  $( document ).ready(function() {
      setInterval(function(){ checkedit(); }, 1000);
  });
  
  function updateidtags(el)
  {
      editingnote = el.id.substr(4);
  }
  
  function checkedit(){
      if(editingnote==-1) return ;
      var curdate = new Date();
      var curtime = curdate.getTime();
      if(editing==1 &amp;&amp; curtime-lastudpdate &gt; 1000){
          updatenote();
      }
  }
  
  function updatenote(){
      var headi = document.getElementById(&quot;inp&quot;+editingnote).value;
      var ent = $(&quot;#entry&quot;+editingnote).html();    
      var entcontent = $(&quot;#entry&quot;+editingnote).text();
      var doss = document.getElementById(&quot;dossier&quot;+editingnote).value;
      var sousdoss = document.getElementById(&quot;sousdoss&quot;+editingnote).value;   
      var tags = document.getElementById(&quot;tags&quot;+editingnote).value;
  
      $.post( &quot;updatenote.php&quot;, {pass: app_pass, id: editingnote, dossier: doss, sousdossier: sousdoss, tags: tags, heading: headi, entry: ent, entrycontent: entcontent, now: (new Date().getTime()/1000)-new Date().getTimezoneOffset()*60})
      .done(function(data){
          if(data=='1'){
              editing = 0;
              $('#lastupdated'+editingnote).html('Last Saved Today');
          }
          else{
              editing = 0;
              $('#lastupdated'+editingnote).html(data);
          }
      });
      $('#newnotes').hide().show(0);
  }
  </code></pre>
  <p>and finaly the update php file is called (I removed from it the part where it send data to the database) :</p>
  <pre><code>&lt;?php
      [...]
          
      $id = $_POST['id'];
      $heading = $_POST['heading'];
      $entry = $_POST['entry'];
      $entrycontent = $_POST['entrycontent'];
      $now = $_POST['now'];
      $seconds = $now;
      $dossier = $_POST['dossier'];
      $sousdossier = $_POST['sousdossier'];
      $tags = $_POST['tags']; 
          
      $filename = &quot;entries/&quot;.$dossier.&quot;/&quot;.$id.&quot;.html&quot;;
          
      [...]
  
      $str = fread($handle, filesize($filename));
      
      if ($entry != ''){
          if (!fwrite($handle, $entry)){
              die(&quot;Error while writing to html file&quot;);
          }  
      }   
      fclose($handle);
  ?&gt;
  </code></pre>
  <p>If it can help to see what the entire code looks like, I can provide the Github repo but I wasn't sure if I could do that here.</p>