简体   繁体   English

js函数调用另一个js函数并返回值问题

[英]js function calling another js function and returning a value problem

i have this JS function: 我有这个JS功能:

function ax_get_new_msg_cnt()
{   var mTimer; 
    var last_msg_id;
    mTimer = setTimeout('ax_get_new_msg_cnt();',30000);
    $.getJSON('/apps_dev.php/profile/newMessageCheck', function(data) 
{
        $('#newMessageDiv').text("You have " + data.msg_cnt + " new ");
        last_msg_id = data.msg_id;
    });
    return last_msg_id;
}

then i have the js function where i call ax_get_new_msg_cnt() : 然后我有js函数,我在其中调用ax_get_new_msg_cnt()

function ax_get_new_msg_details()
{
var mTimer; 
mTimer = setTimeout('ax_get_new_msg_details();',30000);
$.getJSON('/apps_dev.php/messagebox/newMessageDetails', function(data) 
{
    var str='<tr>';
    str += "<td class='td_show_contact_item' align='left' id='td_date'>"+data.td_date+'</td>';
    str += "<td align='left' id='td_subject'><a href='#' style='color:#ff0000 !important' class='spn_small_red_rbc'>"+data.td_subject+"</a></td>";
    str += "<td class='td_show_contact_item' align='left' id='td_from'>"+data.td_from +"</td>";
    //str += "<td id='block_url'>"+data.block_url+"</td>";
    str +='<tr>';
    var tbl = $('#td_date').parents('table');
    var msg_id = ax_get_new_msg_cnt();
    console.log(msg_id);
    if (msg_id == data.td_id)
    {

    }
    else
    {
       $(tbl).append(str);

    }   
});
}

in the above function i get msg_id as undefined... 在上面的函数中我得到msg_id为未定义...
can i do it the way i have it or is there another way please? 我可以用我拥有的方式来做还是请以其他方式来做?
only if msg_id != data.td_id i want to do the append 仅当msg_id != data.td_id我要执行附加
thank you 谢谢

You need to understand the asynchronous way of programming. 您需要了解异步编程方式。

In your first method, when it gets called it starts an async JSON request to the webserver, which will return later in time, but your $.getJSON call return immediately, so your last_msg_id variable will be untouched and this untouched ( undefined ) variable will be returned by ax_get_new_msg_cnt function. 在您的第一个方法中,当它被调用时,它会向网络服务器$.getJSON一个异步JSON请求,该请求将在稍后返回,但是您的$.getJSON调用会立即返回,因此您的last_msg_id变量将保持不变,而该不变的( undefined )变量将被保持不变。由ax_get_new_msg_cnt函数返回。

Later when the webserver returned with the answer and the answer is read by your browser it passes back to your callback function which sets the last_msg_id . 稍后,当网络服务器返回答案并由浏览器读取答案时,它将传递回设置last_msg_id回调函数。

You can write console.log debug messages to see the timings: 您可以编写console.log调试消息以查看计时:

  1. if you insert a console.log('JSON start'); 如果您插入console.log('JSON start'); to your ax_get_new_msg_details method before the $.getJSON call 调用$.getJSON之前的ax_get_new_msg_details方法
  2. then one console.log('Response arrived'); 然后一个console.log('Response arrived'); to inside the function(data) part function(data)部分内部
  3. and an another one right before the end with console.log('JSON request sent'); 最后一个是console.log('JSON request sent');

then, you'll probably see the way of executing. 然后,您可能会看到执行方式。

What ever you want to do with msg_id you need to do it in $.getJSON callback function. 无论您想使用msg_id做什么,都需要在$ .getJSON回调函数中完成。 You can not return it. 您无法退货。 The Ajax call is asyncron, so you have to wait till it is finished to get the value. Ajax调用是asyncron,因此您必须等到完成后才能获取值。

Because the $.getJSON() method is asynchronous, your ax_get_new_msg_cnt() function will return before the data is received, leaving last_msg_id as undefined . 由于$ .getJSON()方法是异步的,因此ax_get_new_msg_cnt()函数将在接收数据之前返回,而last_msg_idundefined

Moreover, I think that the URL parameter for $.getJSON() should point to a file, not to a folder. 而且,我认为$.getJSON()的URL参数应该指向文件,而不是文件夹。

Note: you could improve your code by using the $.ajaxSetup method to set a default timeout for all your jQuery ajax requests. 注意:您可以通过使用$ .ajaxSetup方法为所有jQuery ajax请求设置默认超时来改善代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM