我的 javascript 内存泄漏。以下代码从数据库中获取数据并返回文本框中的值。该数据每秒循环一次并产生内存泄漏。使用开发人员工具,可以看到 xmlHTTP 请求在浏览器上构建。有没有办法解决这个错误?
//the following code gets data from the database
var startVal = 1; //the start value
var stopVal = 451; //the stop value
var presentVal = startVal;
var int = self.setInterval(function () {
getData(presentVal)
}, 1000); //clock cycles every second
function getData(str) //used to get data from the database by calling getdata.php file
{
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
{
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var response = xmlhttp.responseText;
var data = response.split("<>");
var temperature = "";
temperature = data[1];
document.getElementById("temperatureVal").value = temperature; ///Temperature Value
if (presentVal < stopVal) {
presentVal++;
response = "";
} else {
//int=window.clearInterval(int); //stop timer
presentVal = startVal; //start Over
response = "";
}
}
}
xmlhttp.open("GET", "http://localhost/getData.php?index=" + str, true); //calls the getdata.php file
xmlhttp.send();
}
}