简体   繁体   English

如何在javascript中正确执行此操作并在打印出html之前获取所有变量?

[英]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()位移动到成功回调中不会减少它:变量nametype将在每个变量上重新分配循环迭代。 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.

相关问题 如何使用jQuery正确打印HTML中的变量 - How to use jQuery to correctly print out variables in HTML 如何在HTML中绘制多个在所有浏览器中都能正确打印的彩色矩形? - How do I draw multiple coloured rectangles in HTML that print out correctly in all browsers? 如何从一串html字符串中获取JavaScript变量? - How to JavaScript variables out of a string of html?> How to correctly get variables from Django to javascript and do a GET ajax call on a Django URL? - How to correctly get variables from Django to javascript and do a GET ajax call on a Django URL? 如何分割索引,并通过onclick事件将其打印在JavaScript和HTML的新行中 - How do I section out an Index and have it print on a new line in JavaScript and HTML with an onclick event 你如何打印出你输入的输入文本的值? Javascript 和 HTML - How do you print out the the value of input text that you put in? Javascript and HTML 如何在javascript中获取特定HTML标记的所有元素? - How do I get all elements of a particular HTML tag in javascript? 如何获取要在我的HTML中打印的Javascript方法 - How to get Javascript method to print in my HTML 如何让javascript打印出另一个对象中的对象中的属性 - How do I get javascript to print out a property that is in an object in another object 获取所有 Javascript 变量? - Get all Javascript Variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM