使用jQuery, AJAX, Google可视化API和setTimeout()时浏览器内存泄漏
Browser Memory Leak when using jQuery, AJAX, Google Visualization API and setTimeout()
在执行下面的代码时,我注意到浏览器内存泄漏,如果我让仪表板页面加载超过24小时,它会使计算机无响应。
代码应该是相当不言自明的:"hourlyindicators"数据是通过AJAX调用Perl脚本检索,并提供给谷歌可视化API的LineChart小部件,我想实现网页的自动刷新在给定的间隔(设置为5分钟现在)。刷新是有效的,但是存在一个问题,内存消耗随着时间的推移稳步增加。
我需要Javascript闭包吗?抱歉,如果错误是明显的,我是非常新的AJAX/JQuery开发…
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonDatahourlyindicators = $.ajax({
type: "GET",
url: "http://localhost/cgi-bin/hourlyindicators.pl",
dataType: "json",
cache: false,
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var datahourlyindicators = new google.visualization.DataTable(jsonDatahourlyindicators);
// Create data view
var viewfpy = new google.visualization.DataView(datahourlyindicators);
// Select columns to display
viewfpy.setColumns([0,1,2]);
// Instantiate and draw our chart, passing in some options.
var charthourlyfpy = new google.visualization.LineChart(document.getElementById('hourlyindicators_div'));
charthourlyfpy.draw(viewfpy,
{width: 800,
height: 400,
title: 'Today''s Hourly Indicators',
vAxis: {title:"Indicator Value",viewWindowMode:'explicit',viewWindow:{min:minfpy,max:100},textStyle:{color:'black',fontSize:20}},
hAxis: {title:"Hour of Day"},
series: {0:{color:'blue',lineWidth:5,pointSize:10},
1:{color:'green',lineWidth:10,visibleInLegend:false}}
});
// Auto-refreshes every 5 minutes
setTimeout('drawChart()', 5*60*1000);
}
</script>
</head>
<body>
<div id="hourlyindicators_div"></div>
</body>
</html>
更改此代码-我用chrome分析器检查它,它看起来更好
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
var viewfpy = null;
var charthourlyfpy = null;
function drawChart() {
var jsonDatahourlyindicators = $.ajax({
type: "GET",
url: "http://localhost/cgi-bin/hourlyindicators.pl",
dataType: "json",
cache: false,
async: false
}).responseText;
// Create data view
viewfpy = new google.visualization.DataView(datahourlyindicators);
// Select columns to display
viewfpy.setColumns([0,1]);
// Instantiate and draw our chart, passing in some options.
if (charthourlyfpy == null)
{
charthourlyfpy = new google.visualization.LineChart(document.getElementById('hourlyindicators_div'));
}
charthourlyfpy.draw(viewfpy,
{width: 800,
height: 400,
title: 'Today''s Hourly Indicators',
// vAxis: {title:"Indicator Value",viewWindowMode:'explicit',viewWindow:{min:minfpy,max:100},textStyle:{color:'black',fontSize:20}},
hAxis: {title:"Hour of Day"},
series: {0:{color:'blue',lineWidth:5,pointSize:10},
1:{color:'green',lineWidth:10,visibleInLegend:false}}
});
// Auto-refreshes every 5 minutes
setTimeout('drawChart()',1000);
}
</script>
</head>
<body>
<div id="hourlyindicators_div"></div>
</body>
</html>
相关文章:
- 重复应用 d3 转换导致的内存泄漏
- IE7中的blockUI插件内存泄漏25kb
- Javascript闭包-如何防止内存泄漏
- jQuery Draggable:内存泄漏
- "检测到可能的EventEmitter内存泄漏”;使用Gulp+Watchify+Factor捆绑包
- 在Dojo类中递归调用setTimeout时是否存在内存泄漏
- 是内存泄漏
- 将处理程序留在img.onload上是内存泄漏
- 具有并发sse连接的node.js内存泄漏
- 简单对象的Javascript内存泄漏
- WeakMap是否会将我从父/子关系的内存泄漏中拯救出来
- Javascript绘制画布内存泄漏
- Node.js”;检测到EventEmitter内存泄漏”;
- 正在清理内存泄漏
- 递归Javascript对象是否会导致任何问题(内存泄漏)
- Angular JS$编译服务导致$watch内存泄漏
- 如何防止和防范闭包内存泄漏
- 跟踪 JavaScript 内存泄漏的工具
- 页面刷新后javascript内存泄漏有问题吗?为什么?
- XMLHttpRequest循环内存泄漏