admin管理员组文章数量:1414621
Im using the Javascript to read data from digital sensor connected to Raspbery PI (In this case - Rotary Encoder, but it's not that important. This it could be anyone another sensor or controller). In Raspbery PI launched the bash script which write sensor value in "encoder.php" file every time when sensor value is changed. This code working perfectly:
<script src="//ajax.googleapis/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="coords"></div>
<script>
async function fetchData() {
let response = await fetch("encoder.php");
let text = await response.text();
return new Promise(resolve => {
setTimeout(() => resolve(text), 10);
});
}
async function myFunction() {
while (true) {
const data = await fetchData();
document.getElementById("coords").innerHTML = data;
}
}
myFunction();
</script>
Every time then i'm rotate encoder - i'm see the sensor value in "coords" div.
But when i modify this code to (this switching the tabs on page depending on encoder direction):
<script>
async function fetchData() {
let response = await fetch("php/encoder.php");
let text = await response.text();
return new Promise(resolve => {
setTimeout(() => resolve(text), 10);
});
}
async function myFunction() {
while (true) {
const data = await fetchData();
resp = data;
var readState = resp.substr(0, 5);
var direction = resp.substr(6);
if (typeof lastState == 'undefined') {
lastState = ""
};
// console.log(direction);
// console.log(readState);
// console.log(lastState);
if (lastState != readState && lastState != "") { //console.log(direction)
var test = document.getElementsByClassName("active")[0];
// console.log(test);
test2 = test.innerHTML;
// console.log(test2);
var mySubString = test2.substr(10, 6);
// console.log(mySubString);
if (mySubString == "ST_NAV" && direction == "CCW") {
document.getElementById("ST_SCREEN").classList.remove('active');
document.getElementById("SE_SCREEN").classList.add('active');
document.getElementById("ST_nav_el").classList.remove('active');
document.getElementById("SE_nav_el").classList.add('active');
document.getElementById("ST_NAV").classList.remove('active');
document.getElementById("SE_NAV").classList.add('active');
};
if (mySubString == "ST_NAV" && direction == "CW") {
document.getElementById("ST_SCREEN").classList.remove('active');
document.getElementById("MA_SCREEN").classList.add('active');
document.getElementById("ST_nav_el").classList.remove('active');
document.getElementById("MA_nav_el").classList.add('active');
document.getElementById("ST_NAV").classList.remove('active');
document.getElementById("MA_NAV").classList.add('active');
ShowMap();
};
if (mySubString == "SE_NAV" && direction == "CW") {
document.getElementById("SE_SCREEN").classList.remove('active');
document.getElementById("ST_SCREEN").classList.add('active');
document.getElementById("SE_nav_el").classList.remove('active');
document.getElementById("ST_nav_el").classList.add('active');
document.getElementById("SE_NAV").classList.remove('active');
document.getElementById("ST_NAV").classList.add('active');
};
if (mySubString == "MA_NAV" && direction == "CW") {
document.getElementById("DA_SCREEN").classList.add('active');
document.getElementById("MA_SCREEN").classList.remove('active');
document.getElementById("DA_nav_el").classList.add('active');
document.getElementById("MA_nav_el").classList.remove('active');
document.getElementById("DA_NAV").classList.add('active');
document.getElementById("MA_NAV").classList.remove('active');
};
if (mySubString == "MA_NAV" && direction == "CCW") {
document.getElementById("ST_SCREEN").classList.add('active');
document.getElementById("MA_SCREEN").classList.remove('active');
document.getElementById("ST_nav_el").classList.add('active');
document.getElementById("MA_nav_el").classList.remove('active');
document.getElementById("ST_NAV").classList.add('active');
document.getElementById("MA_NAV").classList.remove('active');
};
if (mySubString == "DA_NAV" && direction == "CCW") {
document.getElementById("DA_SCREEN").classList.remove('active');
document.getElementById("MA_SCREEN").classList.add('active');
document.getElementById("DA_nav_el").classList.remove('active');
document.getElementById("MA_nav_el").classList.add('active');
document.getElementById("DA_NAV").classList.remove('active');
document.getElementById("MA_NAV").classList.add('active');
};
if (mySubString == "DA_NAV" && direction == "CW") {
document.getElementById("DA_SCREEN").classList.remove('active');
document.getElementById("RA_SCREEN").classList.add('active');
document.getElementById("DA_nav_el").classList.remove('active');
document.getElementById("RA_nav_el").classList.add('active');
document.getElementById("DA_NAV").classList.remove('active');
document.getElementById("RA_NAV").classList.add('active');
};
if (mySubString == "RA_NAV" && direction == "CCW") {
document.getElementById("DA_SCREEN").classList.add('active');
document.getElementById("RA_SCREEN").classList.remove('active');
document.getElementById("DA_nav_el").classList.add('active');
document.getElementById("RA_nav_el").classList.remove('active');
document.getElementById("DA_NAV").classList.add('active');
document.getElementById("RA_NAV").classList.remove('active');
};
};
lastState = readState;
}
}
myFunction();
</script>
Page normally functioning estimated 2-4 minutes, after this time switching between tabs doesn't happen, as if the function is blocked or freeze. All other dynamic elements on page, continue to work. And page not reloading after pressing refresh or F5 The same if I try to open new tab and enter the page adress. Page not loaded until the tab with the non-working function is closed.
Update 24.02.2025
I rewrite switchig tab algorithm to :
<script>
async function fetchData() {
let response = await fetch("php/encoder.php");
let text = await response.text();
return new Promise(resolve => {
setTimeout(() => resolve(text), 10);
});
}
async function myFunction() {
while (true) {
const data = await fetchData();
var r = data.substr(0, 5);
// var d = data.substr(6,2);
if (typeof l == 'undefined') {
l = ""
};
if (l != r && l != "") {
var i = (Number(data.substr(6, 2)) + Number(document.getElementsByClassName("active")[0].innerHTML.substr(10, 1)));
$('div').removeClass('active');
$('[id="' + i + '"]').addClass('active');
if (i == 2) {
ShowMap();
};
};
l = r;
}
}
myFunction();
</script>
But it not have effect, page still not working after few minutes.
本文标签: javascriptJavaSript request in infinite loop stoped after few minutesStack Overflow
版权声明:本文标题:javascript - JavaSript request in infinite loop stoped after few minutes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745155763a2645162.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论