XMLHttpRequest - release after use?

I am writing a browser application that would be fully managed by AJAX (the first time in my life), this means:

  • it will be one page located in the browser, loading program components if necessary
  • browser history will be, well, no.
  • the page will not be refreshed at all

I care about what I have to do with XMLHttpRequests, as I am primarily a C ++ programmer, which was taught when you write an expression like

x = new XMLHttpRequest();

you need deleteafter that.

This question is entirely devoted to memory management, does this object allocated with help remain newin memory even after it completes its "cycle" with readyState == 4 or is it somehow freed, freed, whatchacallit? Honestly, I have no idea at what point this could be released, since the creation of the script will be in HEAD and sit there potentially all day. Should I:

  • create one or more reusable objects of type XMLHttpRequest, program the application so that it does not need it more than this limit,
  • or does it not matter, and can I allocate as many new XMLHttpRequests as I like?

, , , , "" - . , .

EDIT:

( , ) onClick, XMLHttpRequest :

function submitme(){  
  var p = document.getElementById('p'); //a text field being sent to server
  if(typeof p!='undefined' && p!=null){
    if(p.value!=""){
      //here XMLHttpRequest is created, this function
      //returns exactly object of this type or false, when failed
      var xhr=createXmlHttpRequestObject();
      if(xhr!=false){
        xhr.open("POST", "blablabla.php", true);
        xhr.onreadystatechange=function(){
          if(xhr.readyState==4){
             if(xhr.status==200){
               //result will be posted in this div below
               var adiv = document.getElementById('resultdiv');
               //extract the XML retrieved from the server
               xmlResponse = xhr.responseXML;
               //obtain the document element (the root element) of the XML structure
               xmlDocumentElement = xmlResponse.documentElement;
               //get the response text
               if(typeof adiv !='undefined' && adiv != null){
                  adiv.innerHTML=xmlDocumentElement.childNodes[0].firstChild.nodeValue;
               }
             }//end xhr.status
           }//end xhr.readyState
         };//end xhr.onreadystatechange
         xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         xhr.send(p.value);
       }//end xhr!=false
     }//end p.value!=""
   }//end typeof p!='undefined'
 }//end submitme()

XMLHttpRequest , , xhr, . ? , , none, , readystate==4, onreadystatechange, ? , .

+4
5

JavaScript .

, :

+2

JavaScript , XHR , .

+2

http://xhr.spec.whatwg.org/#garbage-collection - . ( , 100% , ), ,

XMLHttpRequest , , send() , HEADERS_RECEIVED LOADING, : true/

, - readystatechange, progress, abort, error, load, timeout loadend.

, XMLHttpRequestUpload , - , , , , - .

XMLHttpRequest , , .

+2

, , , . , , , .

:

http://javascript.info/tutorial/memory-leaks

"XmlHttpRequest..."

:

IF xhr ( ) xhr onreadystatechange THEN xhr , .

, this xhr (, ) xhr .

+2

XMLHttpRequest . , , .

, XMLHttpRequest - , Ajax.

, XMLHttpRequest Ajax.

, .
, :).

, XMLHttpRequest.
, , XMLHttpRequest ( , ).

0

Source: https://habr.com/ru/post/1525238/


All Articles