0

i have this JS function:

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():

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...
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
thank you

1
  • Eval is Evil! Using a string as the first argument in a setTimeout or setInterval call creates an implicit (and unnecessary) call to eval. Instead of setTimeout('ax_get_new_msg_cnt();', 30000); just pass the function name: setTimeout(ax_get_new_msg_cnt, 30000);. Less code and less computational time used as well. The same thing applies to the setTimeout call in ax_get_new_msg_details. Commented Sep 28, 2011 at 9:25

3 Answers 3

1

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.

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.

You can write console.log debug messages to see the timings:

  1. if you insert a console.log('JSON start'); to your ax_get_new_msg_details method before the $.getJSON call
  2. then one console.log('Response arrived'); to inside the function(data) part
  3. and an another one right before the end with console.log('JSON request sent');

then, you'll probably see the way of executing.

Sign up to request clarification or add additional context in comments.

Comments

0

What ever you want to do with msg_id you need to do it in $.getJSON callback function. You can not return it. The Ajax call is asyncron, so you have to wait till it is finished to get the value.

Comments

0

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.

Moreover, I think that the URL parameter for $.getJSON() should point to a file, not to a folder.

Note: you could improve your code by using the $.ajaxSetup method to set a default timeout for all your jQuery ajax requests.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.