繁体   English   中英

Ajax调用以重新加载Div内存泄漏

[英]Ajax Call to Reload Div Memory Leak

关于如何改善我的问题的任何建议?

因此,我有HTML和Javascript代码,这些数据可以从用于实验室分析统计分析的程序的数据库中提取数据。 例如,您有一个电镀浴,说铜,您需要找出浓度,取电镀浴的pH值,然后将其插入程序中,并保存该信息。 在网页上,它直观地显示了已记录的内容,下一次添加的到期时间以及您需要更改的时间。

因此,我想每三秒钟刷新一次页面,以显示倒数计时,直到进行下一次测试为止,“我已使用下面的代码完成了此操作,但是内存泄漏,您可以看到我尝试了尝试清除内存的事物的数量,但是无论它构建什么,直到页面冻结为止。 谁能看到内存泄漏的问题所在? 在FF和IE 8+尚未测试的情况下发生这种情况,在chrome中没有内存泄漏,但是图像闪烁。

让我提前说,我知道这是一个太长的问题,对不起,但我不知道如何以其他方式提出。 我敢肯定,大家都能看到我对编写Web代码的了解有限,因此我对此表示歉意。 我只是想了解为什么内存会不断增加。 希望有一种方法可以通过更改代码或删除或添加一些代码来修复它。

重新加载脚本。

    function UnloadHandler() { 
window.removeEventListener('unload', UnloadHandler, true); 
}

window.addEventListener('unload', UnloadHandler, true);

jQuery(window).unload(function () { $(window).unbind('unload'); });

jQuery.ajaxSetup ({
cache: false
});

loc = window.location.pathname; //  grabs page url.  \\
pathName = loc.substring(49, loc.lastIndexOf('.') + 4); //  parses out url except for htm file name.  \\

var script = jQuery(document).ready(function() {
var test = setInterval(function(){           //  Sets the data refresh cycle at 3 seconds.  \\
 jQuery.get(pathName, function (response) {
var source = $('<div>' + response + '</div>');
jQuery("#GroupData").empty();
(document.getElementById("GroupData")).innerHTML = "";
 jQuery('#GroupData').html(source.find('#GroupData').load());
timing();  UnloadHandler();
eval(script);

});
},3*1000); //  3 equals seconds to refresh the data thriugh Ajax  \\
}); 

HTML看起来像这样。

<div id="GroupInfo">

    <table style="border:0px; border-style:solid; border-color:#FFFFFF; width:902px; height:20px; border-spacing:0px 1px;border-collapse:seperate; padding:0px; vertical-align:0;">
       <tr>
      <td style="background-color:#FFFFFF;color:000000;background-image: url(../Images/Blue-20x20-Button.png); background-size:30px 22px;width:26px; height:21px;"><div style="color:#000000; text-align:center; margin-bottom:-2px;font-weight:bold;"></div></td>
<td style="width:2px;">&nbsp;</td>
  <td style="background-color:#FFFFFF;color:000000;background-image: url(../Images/Blue-354x20-Button.png); background-size:365px 22px;width:336px; height:21px;"><div style="color:#000000; text-align:center; margin-bottom:-2px; font-weight:bold;">Process Tanks</div></td>

<!--         <td height='10' width='122' bgcolor="#0000FF" ><p align='center'><a><b>Sample</b></td>-->

    <td style="width:14px;">&nbsp;</td>
      <td style="background-color:#FFFFFF;color:000000;background-image: url(../Images/Blue-122x20-ButtonTest.png); background-size:147px 22px;width:135px; height:21px;"><div style="color:#000000; text-align:center; margin-bottom:-2px; font-weight:bold;">Test Status</div></td>
        <td style="width:15px;">&nbsp;</td>
          <td style="background-color:#FFFFFF;color:000000;background-image: url(../Images/Blue-122x20-ButtonTest.png); background-size:147px 22px;width:136px; height:21px;"><div style="color:#000000; text-align:center; margin-bottom:-2px; font-weight:bold;">Adds</div></td>
                <td style="width:16px;">&nbsp;</td>
          <td style="background-color:#FFFFFF;color:000000;background-image: url(../Images/Blue-122x20-ButtonTest.png); background-size:148px 22px;width:134px; height:21px;"><div style="color:#000000; text-align:center; margin-bottom:-2px; font-weight:bold;">Corrective Action</div></td>
               </tr>
         </table>
</div>
<div id="GroupData" align="center">
<div id="DrawRows">
        <table class="DrawRow">
                <tr>
                  <td class="drawrows"></td>
                </tr>
                 </table>
</div>

<SCRIPT type="text/javascript">[ItemsHTML]</script>    

</div>

您还可以看到<SCRIPT type="text/javascript">[ItemsHTML]</script>件事,我不确定这是如何工作的,我没有写,我只是负责更改它。 [ItemsHTML]以某种方式将数据从程序中拉出数据库,并代替[ItemsHTML]如下显示,因此存在带有此代码的源文件,并创建了一个称为out文件和out文件的网页代替;

<SCRIPT type="text/javascript">[ItemsHTML]</script>

变成这个;

<SCRIPT type="text/javascript">DrawRow ("G54.HTM","0","00 Chem/Etch/Gain Calculations","","","","","","","","","42","42");
DrawRow ("G78.HTM","0","00 pH Indicators & Reagent Make-Up","","","","","","","Y","6/14/2014 10:21 AM","99","99");
DrawRow ("G55.HTM","6","01 Aurolectroless SMT 520 ENIG","6/9/2014 5:00 AM","6/9/2014 5:00 AM","6/15/2014 5:00 AM","6/15/2014 10:30 AM","","","Y","6/14/2014 7:59 AM","82","99");
DrawRow ("G56.HTM","127","02 Circuposit™ 3000-1 Process","6/6/2014 9:00 AM","6/6/2014 9:00 AM","6/13/2014 9:00 AM","6/14/2014 12:30 AM","","","Y","6/14/2014 1:56 AM","61","60");
DrawRow ("G57.HTM","36","03 Circubond™ 2200 Alternative Oxide","6/9/2014 5:00 AM","6/9/2014 5:00 AM","6/14/2014 5:00 AM","6/14/2014 11:01 AM","","","Y","6/15/2014 3:59 AM","12","9");
DrawRow ("G58.HTM","2","04 Cupulse™ Acid Copper","6/9/2014 6:00 AM","6/9/2014 6:00 AM","6/13/2014 6:00 AM","6/14/2014 9:25 PM","","","","","6","6");
DrawRow ("G59.HTM","0","05 Electroposit™ 1000 Acid Copper","","","","","","","","","16","16");
DrawRow ("G60.HTM","0","06 Electroposit™ 1100 Acid Copper","","","","","","","","","8","8");
DrawRow ("G61.HTM","36","07 EnviroStrip™ Tin Strippers","6/9/2014 5:00 AM","6/9/2014 5:00 AM","6/12/2014 11:00 AM","6/14/2014 9:30 PM","","","","","9","9");

这不是一个已发布的网站,只需打开“ out”文件并使其在显示器上运行即可完成所有操作,以便员工可以看到制造过程中发生的情况。

计时功能显示页面的更新时间。显示最后一次更改。 同时显示当前时间

    function timing(){          //  Function for setting time formats for Current and Updated Time  \\
    var mydate=new Date()
    var Time = "Current Time:  ";
    var Text = "Last Updated:  ";
    var build = Date.parse('[LastBuild]');
    var build2 = dateFormat(build,"dddd m/dd/yy h:MM:ss TT");  //  Format for updated time.  \\
    var build3 = Date.parse(mydate)
    var build4 = dateFormat(build3, "dddd m/dd/yy h:MM:ss TT");

    document.getElementById('time').innerHTML = Time+" "+ build4; //  Sets the current time to chosen format.  \\

    document.getElementById('time2').innerHTML= Text + " " + build2; //  Sets the updated time to format chosen for build2.  \\
}

我也有此功能,每分钟刷新一次页面。 这几乎可以通过清除大量已分配的内存来解决该问题,但不会释放所有内存。 因此,如果您查看图表上显示的内存,它会上升一分钟,然后下降,但是在30分钟后使用的内存增加了一倍后,并不会一直下​​降。 请参见下面的刷新,无需滚动到顶部。

function refreshPage() {  //  Sets the page refresh to not scroll in IE  \\
var page_y = document.documentElement.scrollTop;
var page_x = document.documentElement.scrollLeft;
window.location.href = window.location.href.split('?')[0] + '?page_y=' + page_y + "&page_x=" + page_x;
}
window.onload = function () {

timing();
UnloadHandler();


setTimeout(refreshPage, 1*60*1000);  //  Change fist number in equation, the first number represents minutes  \\

var match = window.location.href.split('?')[1].split("&");
if (window.location.href.indexOf('page_y') != -1) {
    document.documentElement.scrollTop = match[0].split("=")[1];
}
if (window.location.href.indexOf('page_x') != -1) {
    document.documentElement.scrollLeft = match[1].split("=")[1];
}
     }

观看: http : //www.w3schools.com/jsref/met_win_clearinterval.asp

我认为您的内存泄漏来自您未使用的clearInterval。 每次使用带有间隔的递归函数时,都必须清除预览调用(以避免内存泄漏)。

最好的祝福

我们将逐步执行此操作:)

删除eval(script); (不能使用eval,请检查stackoverflow讨论: 为什么使用JavaScript eval函数是个坏主意?

相反,只需调用一个声明的函数。 这是修改后的代码:

    var timer = '';
jQuery.ajaxSetup ({
cache: false
});
loc = window.location.pathname;//  grabs page url.  \\
pathName = loc.substring(49, loc.lastIndexOf('.') + 4); //  parses out url except for htm file name.  \\
function script(){
timer = setInterval(function(){//  Sets the data refresh cycle at 3 seconds.  \\
clearInterval(timer);
jQuery.get(pathName, function (response) {
var source = $('<div>' + response + '</div>');
jQuery("#GroupData").empty();
(document.getElementById("GroupData")).innerHTML = "";
jQuery('#GroupData').html(source.find('#GroupData').load());
timing();
$(window).unbind('unload');
script();
});
},3*1000); //  3 equals seconds to refresh the data thriugh Ajax  \\
}

jQuery(document).ready(function() {
script();
}); 

修改后,“ timing()”函数做什么? 你能过去代码吗? 也许该功能也会造成内存泄漏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM