[英]getJSON variable returns undefined
I don't know why 'streamName[i]' inside my getJSON is returning 'undefined'. 我不知道为什么getJSON中的'streamName [i]'返回'undefined'。 Everything inside it returns the correct values but only the streamName one return the undefined
它里面的所有内容都返回正确的值,但只有streamName返回未定义的值
var streamName = ['LCK1', 'ryan_clark', 'syndicate', 'riotgames', 'esl_csgo', 'Nightblue3', 'summit1g', 'imaqtpie', 'sodapoppin', 'captainsparklez']; var nullLogo = "https://dummyimage.com/50x50/ecf0e7/5c5457.jpg&text=0x3F"; var name; for (var i = 0; i < streamName.length; i++) { var url = 'https://api.twitch.tv/kraken/streams/' + streamName[i] + '?callback=?'; $.getJSON(url, function(data) { console.log(name); if (data.stream == null) { $('.streamersList').append('<div> <div class="logo"> <img src=' + nullLogo + '></div> <div class="nameStreamer">' + streamName[i] + '</div> <div class="state"> Offline </div></div>'); } else { $('.streamersList').append('<div> <div class="logo"> <img src=' + data.stream.channel.logo + '></div> <div class="nameStreamer">' + streamName[i] + '</div> <div class="state">' + data.stream.channel.game + ' </div></div>'); } }); }
Because $.getJSON
is an asynchronous function, by the time the callback runs, i
will have completed the loop. 因为
$.getJSON
是一个异步函数,所以在回调运行时, i
将完成循环。 Since the loop breaks when i
is greater than or equal to the length of streamName
, i
will be trying to access an element in streamName
past the end of the array, which is undefined
. 由于当
i
大于或等于streamName
的长度时循环中断,因此i
将尝试通过数组末尾( undefined
访问streamName
的元素。
The reason i
is, in this case, 10 inside every instance of the callback is because of the way scoping works in JavaScript. 在这种情况下,
i
之所以在每个回调实例内部包含10个原因,是因为作用域在JavaScript中起作用。 As far as the code is aware, i
is declared at the top of the function along with streamName
, nullLogo
, and name
. 据代码所知,
i
与streamName
, nullLogo
和name
一起在函数的顶部声明。 While iterating through the loop, the value of i
is changed and that change is visible everywhere inside the function, including inside the callbacks, which have not run yet. 在循环中进行迭代时,
i
的值会更改,并且该更改在函数内部的所有位置(包括尚未运行的回调内部)都可见。 By the time they do run, i
will be 10 because it reached the end of the loop, and that is the value the callbacks will use. 到它们运行时,
i
将为10,因为它已到达循环的结尾,这就是回调将使用的值。
One way to make sure you are getting the correct value for i
inside the $.getJSON
function is to pass i
as a parameter to an immediately-invoked function. 确保在
$.getJSON
函数中获得正确的i
值的一种方法是将i
作为参数传递给立即调用的函数。 This will effectively bind the current value of i
to the parameter index
, so using index
to get an element out of the array will have the correct value based on the iteration of the loop. 这将有效地将
i
的当前值绑定到参数index
,因此基于循环的迭代,使用index
将元素从数组中取出将具有正确的值。
for (var i = 0; i < streamName.length; i++) {
// note how i can be used here because this is synchronous, aka happening right now
var url = 'https://api.twitch.tv/kraken/streams/' + streamName[i] + '?callback=?';
(function(index) {
$.getJSON(url, function(data) {
// this is asynchronous (happens in the future), so i will have a different
// value by the time it is called, but index will have the correct value
console.log(name);
if (data.stream == null) {
$('.streamersList').append('<div> <div class="logo"> <img src='
+ nullLogo
+ '></div> <div class="nameStreamer">'
+ streamName[index]
+ '</div> <div class="state"> Offline </div></div>');
} else {
$('.streamersList').append('<div> <div class="logo"> <img src='
+ data.stream.channel.logo
+ '></div> <div class="nameStreamer">'
+ streamName[index]
+ '</div> <div class="state">'
+ data.stream.channel.game
+ ' </div></div>');
}
});
})(i);
}
$.getJSON
is an async function. $.getJSON
是一个异步函数。 Thereby, when the function callback happens, the loop has already looped through all iterations of i. 因此,当函数回调发生时,循环已经循环了i的所有迭代。 So i's value in your case is
streamName.lenghth
Which makes streamName[i]
undefined 因此,在您的情况下,我的值是
streamName.lenghth
这使streamName[i]
未定义
I would avoid using an index in a callback like this. 我会避免在这样的回调中使用索引。 What you'll need to use is an immediately invoked function express(IIFE) Checkout this other stackoverflow post.
您需要使用的是立即调用的函数express(IIFE)签出此其他stackoverflow帖子。 How to access index variable in a jquery getJson call ($.getJson) during a loop?
如何在循环期间访问jquery getJson调用($ .getJson)中的索引变量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.