[英]How do this correctly in javascript and get all variables before print out to html?
i write some function, and i called abApi.general.getUserById
function, but when i print output to $("#friend-requests-block")
my name
variable is empty. 我写了一些函数,并调用了abApi.general.getUserById
函数,但是当我将输出打印到$("#friend-requests-block")
我的name
变量为空。 i know this is javascript and i try with $("#friend-requests-block")
in getUserById
callback, but then i haven't type
. 我知道这是javascript,我尝试在getUserById
回调中使用$("#friend-requests-block")
,但随后我没有type
。 How can i fix this? 我怎样才能解决这个问题?
for (var index in requests) {
var name = "";
var type = "";
if(requests[index].FriendRequestTypeId == 1) {
type = "private";
}
else {
type = "business";
}
abApi.general.getUserById(abEnvironment.sessionToken, requests[index].FromUserId, function(response2){
name = response2.Name;
});
$("#friend-requests-blocks").html($("#friend-requests-blocks").html() + "<div class=\"log-block\" id=\"friend-requests-log-"+requests[index].Id+"\"><a href=\"#\"><h2>"+ name + "("+ type +")</h2></a> <div class=\"friend-requests-buttons\" style=\"margin-top: 15px;\"> <a href=\"javascript:void(0)\" data-role=\"button\" style=\"margin: 0px 5px 0px 5px;\" onClick=\"abAction.approveFriendRequest("+requests[index].Id+", 1,"+requests[index].FromUserId+");\">Accept</a><a href=\"javascript:void(0)\"data-role=\"button\" style=\"margin: 0px 5px 0px 5px;\" onClick=\"abAction.approveFriendRequest("+requests[index].Id+", 0,"+requests[index].FromUserId+");\">Reject</a> <a class=\"button3\" href=\"javascript:void(0)\"data-role=\"button\" style=\"margin: 0px 5px 0px 5px;\" onClick=\"abAction.approveFriendRequest("+requests[index].Id+", 2,"+requests[index].FromUserId+");\">Later</a></div></div>");
}
It's not just a scope issue you're dealing with, but the fact that getUserById
is async, too (even more than the scope). 这不仅是您要处理的范围问题,而且getUserById
也是异步的(甚至超出范围)。 the variable name
won't be set until your script receives a response. 在脚本收到响应之前,不会设置变量name
。 Having said that, since you're using a loop, moving the $('#friend-request-blocks').html()
bit to the success callback won't cut it: the variables name
and type
will be reassigned on every loop iteration. 话虽如此,由于您使用的是循环,因此将$('#friend-request-blocks').html()
位移动到成功回调中不会减少它:变量name
和type
将在每个变量上重新分配循环迭代。 To get around this, you'll have to use a closure: 为了解决这个问题,您必须使用闭包:
abApi.general.getUserById(abEnvironment.sessionToken,requests[index].FromUserId,
(function(type)
{//pass the current type as an argument to closure
return function(response2)
{
var name = response2.Name;//declare local variable name, or use response2.Name
$('#friend-request-blocks').html('html goes here with correct type: '+type+' and name: '+name);
};
}(type)));
Also, I get the impression you're looping through an array, not an object, though I could be wrong. 另外,我得到的印象是您正在遍历数组而不是对象,尽管我可能是错的。 If requests
is an array, it's best not to use a for...in
loop, but a regular for(var i;i<requests.length;i++)
loop. 如果requests
是数组,则最好不要使用for...in
循环,而应使用常规的for(var i;i<requests.length;i++)
循环。 Google will give you a huge list of reasons as to why for...in
on arrays isn't the best of ideas. Google会为您提供大量的理由,说明为什么for...in
数组中并不是最好的主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.