XMLHttpRequest-使用后释放? [英] XMLHttpRequest - freeing after use?

查看:266
本文介绍了XMLHttpRequest-使用后释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个完全由AJAX驱动的浏览器应用程序(这是我生命中的第一次),这意味着:

  • 它将是停留在浏览器中的一页,根据需要加载程序组件
  • 浏览器历史记录将完全消失.
  • 页面完全不会刷新

我担心的是我应该如何处理XMLHttpRequests,因为我主要是C ++程序员,所以被告知在编写

之类的语句时

  x = new XMLHttpRequest(); 

您之后需要删除.

这个问题完全是关于内存管理的,是否用 new 分配的对象是否仍保留在内存中,即使完成后它的状态为readyState == 4的周期",还是以某种方式释放,释放了whatchacallit?老实说,我不知道在什么时候可以释放它,因为创建这些脚本的脚本将放在HEAD中,并且可能整天呆在那里.我是否应该:

  • 创建一个或多个XMLHttpRequest类型的可重用对象,对该应用进行编程,以使其不再需要此限制,
  • 还是没关系,我可以根据需要分配尽可能多的新XMLHttpRequest?

请考虑在我的网页的框架"保持原样的情况下,在什么时候删除这些对象以及为什么删除这些对象,原因为何?希望我将这个问题弄清楚,并感谢您的深刻见解.

考虑一个 onClick 事件处理程序的代码(我删除了许多行,这些行为简洁起见检查了意外的返回值),该事件处理程序创建XMLHttpRequest并将其发送:

 函数commitme(){var p = document.getElementById('p');//一个文本字段被发送到服务器if(typeof p!='undefined'&& p!= null){if(p.value!="){//在这里创建XMLHttpRequest,此函数//如果失败,则精确返回此类型的对象或falsevar xhr = createXmlHttpRequestObject();if(xhr!= false){xhr.open("POST","blablabla.php",true);xhr.onreadystatechange = function(){if(xhr.readyState == 4){if(xhr.status == 200){//结果将在下面的此div中发布var adiv = document.getElementById('resultdiv');//提取从服务器检索到的XMLxmlResponse = xhr.responseXML;//获取XML结构的document元素(根元素)xmlDocumentElement = xmlResponse.documentElement;//获取响应文本if(typeof adiv!='undefined'&& adiv!= null){adiv.innerHTML = xmlDocumentElement.childNodes [0] .firstChild.nodeValue;}}//结束xhr.status}//结束xhr.readyState};//结束xhr.onreadystatechangexhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");xhr.send(p.value);}//结束xhr!= false}//结束p.value!="}//结束typeof p!='未定义'}//结束submitme() 

创建XMLHttpRequest对象实例时,如果触发了此处理程序,则 xhr 变量将对其进行一次引用,直到处理程序执行完毕.此时,此对象实例有多少个引用?如果我正确理解了您的答案中链接的文章,则答案应该为,浏览器仅等待此请求打开 readystate == 4 ,完成执行 onreadystatechange函数和对象是否不可达?请确认.

解决方案

JavaScript具有自动内置的垃圾收集器.

您可能想阅读文章:

I'm writing a browser app that would be entirely AJAX-driven (first time in my life), that means:

  • it will be one page staying in the browser, loading program components as needed
  • browser history will be, well, none.
  • page will not be refreshed at all

My concern is what should I do about XMLHttpRequests, since I'm primarily C++ programmer, being taught that when you write a statement like

x = new XMLHttpRequest();

you need to delete it afterwards.

This question is entirely about memory management, whether this object, allocated with new stays in memory, even after it finishes it's "cycle" with readyState == 4 or is somehow released, freed, whatchacallit? Honestly, I have no idea at what point could it be freed, since the script creating these will be in HEAD and sit there potentially whole workday. Whether should I:

  • create one or several, reused objects of type XMLHttpRequest, program the app so that it won't need any more than this limit,
  • or it doesn't matter and I can allocate as many new XMLHttpRequests as I like?

Please include in your answers at what point and WHY those objects would be deleted, if they would at all, considering that the "frame" of my webpage will stay. Hope I made this question clear and thanks for any insightful answers.

EDIT:

Consider code (I removed many lines that were checking for unexpected return values for brevity) of an onClick event handler, that creates XMLHttpRequest and sends it:

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()

When XMLHttpRequest object instance is created, if this handler is fired, it is referenced once by xhr variable until handler finishes to execute. At this point there are how many references to this object instance? If I understand articles linked in your answers correctly, the answer should be none, and browser just waits for this request to turn readystate==4, finish executing onreadystatechange function and object is unreachable? Please confirm.

解决方案

JavaScript has an automatic built-in garbage collector.

You might want to read this article:

这篇关于XMLHttpRequest-使用后释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆