[英]JS. while loop crashes tab
I'll go crazy. 我会发疯的。 I am writing code that cyclically gets data from the server and if I use a while loop:
我正在编写循环从服务器获取数据的代码,如果我使用while循环:
var i = 100;
while (200>i) {
zz.api("get", {"offset": i, "v": 5.14}, function(data) {
ids = ids.concat(data.response.items.map(maps));
i= i + 100;
alert(ids.length);
});
}
In chrome Tab stops responding, Firefox crashes. 在chrome中,Tab停止响应,Firefox崩溃。
If I use for loop 如果我使用for循环
var i = 100;
for (i=100; i<200; i= i+100) {
zz.api("get", {"offset": i, "v": 5.14}, function(data) {
ids = ids.concat(data.response.items.map(maps));
alert(ids.length);
});
}
The script is executed correctly. 该脚本已正确执行。
WHAT am I doing wrong? 我究竟做错了什么? I can not understand, why browser dies from "while" looping.
我不明白,为什么浏览器死于“ while”循环。
The same result as for
can be done with 相同的结果
for
是可以做到的
var i = 100;
while (200>i) {
zz.api("get", {"offset": i, "v": 5.14}, function(data) {
ids = ids.concat(data.response.items.map(maps));
alert(ids.length);
});
i= i + 100;
}
In the other case, while
keeps executing infinitely until async
call is finished and callback is called, but as this loop goes infinite callback will never get called in javascripts single threaded execution and i never gets incremented. 在另一种情况下,
while
持续执行无限,直到async
调用完成并调用了回调,但是随着循环的进行,无限回调将永远不会在javascripts单线程执行中被调用,而我也永远不会递增。 Meanwhile infinite number of zz.api
is called resulting in many ajax calls which helps in even easier crashing. 同时,无限数量的
zz.api
被调用,导致许多ajax调用,这有助于更轻松地崩溃。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.