我正面对" aw snap!google chrome在尝试显示网页时用完了记忆。"
下面是我在每一秒钟调用API的简单应用。每次调用后,该选项卡的Chrome内存分配大小将连续增加。但不要减少记忆。在引起页面崩溃问题的最后一个。
<!DOCTYPE html>
<html>
<body>
<div id="time">
<h1>Time Sample Timer App</h1>
<button type="button" onclick="getTime()">Start Timer</button>
</div>
<script>
function getTime() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("time").innerHTML =
JSON.parse(this.responseText).time;
setTimeout(getTime, 1000);
}
};
xhttp.open("GET", "http://date.jsontest.com", true);
xhttp.send();
}
</script>
</body>
</html>
请帮助我找出此根本原因。
我还在AW Snap上发布了我的观察错误!Google Chrome在尝试显示网页时用完了记忆。
但是从Google方面,没有人回答我的应用程序或Google Chrome本身的实际问题。
问题是内存中的n settimeout数量。它持有它,然后您看到您收到的错误消息。解决方案是使用命名timeout 变量,只有在分配新的之前,请从内存中清除它。p>
var time; // declare a time
function getTime() {
if (time) {
clearTimeout(time);
} // clear if there is any prev setTimeout running
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("time").innerHTML = JSON.parse(this.responseText).time;
time = setTimeout(getTime, 1000); // assign a new setTimeout
}
};
xhttp.open("GET", "http://date.jsontest.com", true);
xhttp.send();
}
<div id="time">
<h1>Time Sample Timer App</h1>
<button type="button" onclick="getTime()">Start Timer</button>
</div>