<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[使用jQuery Ajax功能时需要注意的一个问题(内存溢出)，Ajax使用后要记得句柄销毁，特别是7X24小时不间断的调ajax。]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 06 Oct 2015 13:35:56 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：最近搞Raspberry Pi下的ajax从串口里取数据，需要7X24Hour，3秒一次,用chrome下的js分析工具发现内存有不断增加的情况，Raspberry Pi 512M。<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;最近一哥们在做一个Ajax长连接的项目，页面需要和服务器保持长连接，而且在连接超时后需要重新请求连接，过程中他问我要用到什么，我也是想都没想就告诉他用jQuery。jQuery不是有ajaxSuccess ajaxError这些对象吗，在请求完成或者请求失败后重新请求不就好了。 <br/><br/>但是后来他告诉我说没有用 jQuery，自己手工写的XMLhttprequest 。他告诉我说，开始是用jquery写的，而且在测试过程中也没有出现问题。但是在后来无意中发现，在页面开的时候久了之后，浏览器资源竟然占用非常高导致内存不足而崩溃了。后来抓包分析发现，每次jquery的Ajax请求都会创建一个xmlHttprequest对象，理论上讲，长连接的请求是一个无限递归，请求数量是非常大的，但是由于每次请求都会建立一个新的xmlhttprequest，而且jquery不会自动回收资源，所以导致了内存溢出。 <br/><br/>通过查看jquery API，发现jquery还有一个 complete对象，是请求完成后回调函数 (请求成功或失败之后均调用)。 同时有两个参数XMLHttpRequest, textStatus。所以，我们只需要在请求完成后，将传回的XMLHttprequest对象手工回收即可，代码如下： <br/>代码如下:<br/><textarea name="code" class="php" rows="15" cols="100">
$.ajax(&#123; 
url: &quot;http://www.jb51.net&quot;, 
data: &#123; name: &quot;xxxx&quot; &#125;, 
dataType: &quot;xml&quot;, 
success: function (data, textStatus) &#123; 
//do something... 
&#125;, 
complete: function (XHR, TS) &#123; XHR = null &#125; 
&#125;); 

</textarea><br/><br/><br/>来自：http://www.jb51.net/article/30458.htm<br/><br/> 原生态的写法如下：<br/><textarea name="code" class="php" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;var xhr = new XMLHttpRequest(); 
&nbsp;&nbsp;&nbsp;&nbsp;xhr.open(/* method */ &quot;POST&quot;, 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* target url */ &quot;/upload&quot;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*, async, default to true */,true); //改为同步就按顺序了：否则文件传对了，但是Nginx文件的stat文件还存在。也就是说明这个nginx的断点模块可能对多进程同时上传同一文件不同分片的处理不是太好。
&nbsp;&nbsp;&nbsp;&nbsp;//xhr.setRequestHeader(&quot;Content-Length&quot;,&quot;100&quot;); //这个设置没啥用处
&nbsp;&nbsp;&nbsp;&nbsp;//xhr.overrideMimeType(&quot;application/octet-stream&quot;); 
&nbsp;&nbsp;&nbsp;&nbsp;xhr.setRequestHeader(&quot;Content-Type&quot;,&quot;application/octet-stream&quot;); 
&nbsp;&nbsp;&nbsp;&nbsp;xhr.setRequestHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&#92;&quot;big.TXT&#92;&quot;&quot;);

&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;startRange = start;//file.slice(0, 10);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;endRange = end-1;&nbsp;&nbsp;//bytes 0-9/81==&gt;10-19
&nbsp;&nbsp;&nbsp;&nbsp;maxFileSizeFlag = fileSize -1;
&nbsp;&nbsp;&nbsp;&nbsp;if(endRange &gt;= maxFileSizeFlag)&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;endRange=fileSize-1;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;//xhr.setRequestHeader(&quot;Content-Length&quot;,endRange-startRange); //这是从0开始
&nbsp;&nbsp;&nbsp;&nbsp;xcontentrange = &quot;bytes &quot;+startRange+&quot;-&quot;+endRange+&quot;/&quot;+fileSize;
&nbsp;&nbsp;&nbsp;&nbsp;start = start + oneSliceLen;//下次分片范围头
&nbsp;&nbsp;&nbsp;&nbsp;end = end + oneSliceLen;//下次分片范围尾
&nbsp;&nbsp;&nbsp;&nbsp;//xhr.setRequestHeader(&quot;X-Content-Range&quot;, &quot;bytes 0-9/81&quot;); //这是从0开始
&nbsp;&nbsp;&nbsp;&nbsp;xhr.setRequestHeader(&quot;X-Content-Range&quot;, xcontentrange); //这是从0开始
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;xhr.setRequestHeader(&quot;Session-ID&quot;, sessionId);
&nbsp;&nbsp;&nbsp;&nbsp;xhr.send(packet); 
&nbsp;&nbsp;&nbsp;&nbsp;delete packet;

&nbsp;&nbsp;&nbsp;&nbsp;xhr.onreadystatechange = function() &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (xhr.readyState == 4) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (xhr.status == 200) &#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(&quot;upload complete&quot;); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//console.log(&quot;response: &quot; + xhr.responseText); 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//document.getElementById(&quot;responseText&quot;).textContent=document.getElementById(&quot;responseText&quot;).textContent+&quot;&#92;r&#92;n&quot;+xhr.responseText;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document.getElementById(&quot;file&quot;).value=&quot;&quot;;//清空file里的文件，防止重复提交，没有F5把全局变量清空引起上传的长度错误。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(&quot;提示：文件成功上传，传第二个文件时请重新F5刷新该页并重新选择文件后测试分片上传。&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start=0;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (xhr.status == 201) &#123;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//document.getElementById(&quot;responseText&quot;).textContent=document.getElementById(&quot;responseText&quot;).textContent+&quot;&#92;r&#92;n&quot;+xhr.responseText;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125; 
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;xhr.complete = function()&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xhr=null;
&nbsp;&nbsp;&nbsp;&nbsp;&#125;

</textarea>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] 使用jQuery Ajax功能时需要注意的一个问题(内存溢出)，Ajax使用后要记得句柄销毁，特别是7X24小时不间断的调ajax。]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>