gpt4 book ai didi

javascript - 异步编程中的for循环

转载 作者:行者123 更新时间:2023-11-30 16:02:03 25 4
gpt4 key购买 nike

<分区>

userId = ['123', '456'];
userName = ['aaa', 'bbb'];
for (var j = 0; j < userId.length; j++) {
Instagram.otherUserMedia(userId[j], function (response) {
$('#otherInfo').append('<h2>' + userName[j] + '</h2>');
for(var i = 0; i < response.data.length; i++) {
$('#otherInfo').append('<img src="' + response.data[i].images.thumbnail.url + '" />');
}
});
}

在此代码片段中,我需要显示相应的 userName 以及来自 response 的输出图像。但是当我执行这段代码时,j 的值递增直到 userId.length 然后进入回调。所以,当我想显示 userName[j] 时,它说 j is undefined 因为它循环了 userId 中的每个值。我想为每个 userIdresponse 获取相应的 userName

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