简体   繁体   English

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

[英]Ajax Call to Reload Div Memory Leak

Any Suggestions on how i can make my question better? 关于如何改善我的问题的任何建议?

So I have HTML and Javascript code that takes data out of a DB of a program that is used for statistical analysis of Lab Analysis. 因此,我有HTML和Javascript代码,这些数据可以从用于实验室分析统计分析的程序的数据库中提取数据。 For example you have a plating bath say copper and you need to find out the concentration, you take the pH of the bath you then plug it into the program and it saves that information. 例如,您有一个电镀浴,说铜,您需要找出浓度,取电镀浴的pH值,然后将其插入程序中,并保存该信息。 On the webpage it shows you visually what has been recorded, when the next add is due and how long you have to make changes. 在网页上,它直观地显示了已记录的内容,下一次添加的到期时间以及您需要更改的时间。

So I then would like to refresh the page every three seconds to show the times counting down till the next test is due, 'I have accomplished this with the code below, however there is a memory leak and as you can see i have tried a number of things to try to clear the memory, but no matter what it builds till the page freezes. 因此,我想每三秒钟刷新一次页面,以显示倒数计时,直到进行下一次测试为止,“我已使用下面的代码完成了此操作,但是内存泄漏,您可以看到我尝试了尝试清除内存的事物的数量,但是无论它构建什么,直到页面冻结为止。 Can anyone see what the issue is with the memory leak? 谁能看到内存泄漏的问题所在? It happens in FF and IE 8+ havent tested below that, in chrome no memory leak, but the images flash. 在FF和IE 8+尚未测试的情况下发生这种情况,在chrome中没有内存泄漏,但是图像闪烁。

Let me say in advance, I know this is too long a question, so I am sorry, but I don't know how to ask it any other way. 让我提前说,我知道这是一个太长的问题,对不起,但我不知道如何以其他方式提出。 As I am sure all of you can see I have a limited understanding of writting web code, so I apologize for that too. 我敢肯定,大家都能看到我对编写Web代码的了解有限,因此我对此表示歉意。 I'm just trying to understand why the memory builds and builds. 我只是想了解为什么内存会不断增加。 Hopefully there is a way to fix it, by changing the code or removing or adding some code. 希望有一种方法可以通过更改代码或删除或添加一些代码来修复它。

To Reload script. 重新加载脚本。

    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  \\
}); 

The HTML looks like this. 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>

One more thing where you see <SCRIPT type="text/javascript">[ItemsHTML]</script> I'm not exactly sure how this works, I didn't write I'm just in charge of changing it. 您还可以看到<SCRIPT type="text/javascript">[ItemsHTML]</script>件事,我不确定这是如何工作的,我没有写,我只是负责更改它。 the [ItemsHTML] somehow pulls the data out of the DB from the program and displays it as shown below in place of [ItemsHTML] so there is a source file with this code and it creates a webpage called an out file and in the out file instead of; [ItemsHTML]以某种方式将数据从程序中拉出数据库,并代替[ItemsHTML]如下显示,因此存在带有此代码的源文件,并创建了一个称为out文件和out文件的网页代替;

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

it becomes this; 变成这个;

<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");

This is not a published website it is all done by opening the "out" file and letting it run on a display so employees can see what is going on with the manufacturing process. 这不是一个已发布的网站,只需打开“ out”文件并使其在显示器上运行即可完成所有操作,以便员工可以看到制造过程中发生的情况。

Timing function displays an updated time for the page.. showing the last change made. 计时功能显示页面的更新时间。显示最后一次更改。 Also it shows current time 同时显示当前时间

    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.  \\
}

I also have this to refresh the page ever minute. 我也有此功能,每分钟刷新一次页面。 This almost fixes the problem by clearing a lot of the allocated memory built up, but it doesnt release all of it. 这几乎可以通过清除大量已分配的内存来解决该问题,但不会释放所有内存。 So if your looking at the memory profiled on a graph it goes up for a minute then dips down, but not all the way down after 30 minutes te memory used has doubled. 因此,如果您查看图表上显示的内存,它会上升一分钟,然后下降,但是在30分钟后使用的内存增加了一倍后,并不会一直下​​降。 See below for refresh with no scroll to top. 请参见下面的刷新,无需滚动到顶部。

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];
}
     }

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

I think your Memory leak comes from a clearInterval that you don't use. 我认为您的内存泄漏来自您未使用的clearInterval。 Every time you use a recursive function with interval, you have to clear previews calls (for avoid memory leak). 每次使用带有间隔的递归函数时,都必须清除预览调用(以避免内存泄漏)。

Best regards 最好的祝福

We are going to do this by steps :) 我们将逐步执行此操作:)

Remove the eval(script); 删除eval(script); (eval must not be used, check stackoverflow discussion : Why is using the JavaScript eval function a bad idea? ) (不能使用eval,请检查stackoverflow讨论: 为什么使用JavaScript eval函数是个坏主意?

Instead, just call a declared function. 相反,只需调用一个声明的函数。 Here is your code modified : 这是修改后的代码:

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

After modifying this, what does the "timing()" function do ? 修改后,“ timing()”函数做什么? can you past your code ? 你能过去代码吗? Maybe the function creates a memory leaks too. 也许该功能也会造成内存泄漏。

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

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