为什么这个while循环无限循环? [英] Why is this while loop looping infinitely?
问题描述
所以我正在为我的研究制作一个网络应用程序,它基本上结合使用 Twitter 的流媒体 API 和谷歌地图来显示 Lat Lng 值.
So i'm making a web app for my studies, it basically uses a combination of Twitter's streaming api and Google Maps to display the Lat Lng values.
Y 是一个元素,表示已收到的推文数量,(检查数组的长度),X 是另一个元素,它的文本内容每秒递增 1.
Y is an element which states the number of tweets which have been received, ( checks length of an array) and X is another which has it's text content incremented by 1 every second.
- 我只是为它花费的时间设置了一些随机值,我会使用更合乎逻辑的比例(每 60 秒或其他时间).
我想每分钟记录一次这些信息,以制作图表,但不知何故我做了一个无限循环,我不太确定如何.
I wanted to log this information every minute, to make a graph but i've made an infinite loop somehow and i'm not quite sure how.
代码如下:
function saveinfo(){
var x = document.getElementsByClassName('timer')[0].textContent;
var y = document.getElementsByClassName('counter')[0].textContent;
while (x != "510"){
if(x = "5"){
console.log(y);
}
else if (x = "200") {
console.log(y);
}
else if (x = "300"){
console.log(y);
}
else if (x = "400"){
console.log(y);
}
else if (x = "500"){
console.log(y);
x = "510"
}
}
};
推荐答案
当你的 if
语句被评估时,你正在设置 x
:
When your if
statements are evaluated, you're setting x
:
if(x = "5"){
这意味着 x
永远不会等于 "510"
,你的循环将永远持续下去.
This means that x
will never equal "510"
, and your loop will go on forever.
要解决此问题,您应该执行类似 if(x === '5')
To fix this, you should do something like if(x === '5')
这篇关于为什么这个while循环无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!