gpt4 book ai didi

javascript - setInterval 在 Android 版 Firefox 中持续运行

转载 作者:行者123 更新时间:2023-12-03 05:52:15 25 4
gpt4 key购买 nike

我有一小段 Javascript,我想每隔几秒轮询一次服务器并更新 DOM。

function updateCard() {    
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
card = JSON.parse(this.responseText);
document.getElementById("season").innerHTML = card.season;
}
};
xhttp.open("GET", "/curr_card/", true);
xhttp.send();
}
window.onload = updateCard;
window.setInterval(updateCard,2000);

在大多数浏览器上都会发生这种情况。有一些对 updateCard 的一次性调用,但总体而言,服务器显示每个客户端每秒约 1/2 个连接。

但是,当我在 Android (49.0) 上的 Firefox 中访问该页面时,浏览器开始连续轮询 /curr_card/,每秒数十次。

我看到有人建议将 setInterval 行替换为 window.setInterval(function() {updateCard();},2000);,这没有帮助。

我对 Javascript 和 AJAX 还很陌生,所以不知道为什么会发生这种情况。是FF的bug吗?如果需要,我可以发布更多代码。

提前致谢。

最佳答案

经过 OP 评论中的测试和讨论,我们得出结论,这一定是 OP 的 HTC M7 上的 Firefox 特有的问题,因为它无法在 Galaxy S7 上的同一版本 Firefox 上重现。

关于javascript - setInterval 在 Android 版 Firefox 中持续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40098218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com