heron
heron

Reputation: 3661

Variable is undefined error (even if console.log shows variable)

My function looks like that

var mail_ntfy=$("#nav_mail"), question_ntfy=$("#nav_question"), users_ntfy=$("#nav_users");
function CheckAll(){
    var data=checkFor("m,q,u");
    if(mail_ntfy.attr("data-number")!=data.m_count && data.m_count!=0)
        mail_ntfy.attr("data-number", data.m_count);
    if(question_ntfy.attr("data-number")!=data.q_count && data.q_count!=0)
        question_ntfy.attr("data-number", data.q_count);
    if(users_ntfy.attr("data-number")!=data.u_count && data.u_count!=0)
        users_ntfy.attr("data-number", data.u-count);
    showNotes(data.msg);
    chngTitle(data.msg);    
}

$(document).ready(function () {
    setInterval(CheckAll(), 10000);
})

function checkFor(param){    
    $.ajax({
        url: "core/notifications.php",
        type: "POST",
        dataType: "json",
        data: {
            chk:param
        },
        success: function (data) { 
            if(data.status!="error")  {
                console.log(data);
                return data;                
            }

        }
    });
}

I got 2 questions:

1) I see that, checkFor function returns result (console.log shows result) but still getting data is undefined error message on line if(mail_ntfy.attr("data-number")!=data.m_count && data.m_count!=0). What am I missing?

2) I want to execute, CheckAll in every 10 seconds. But it doesn't start more than 1 time. why setinterval doesn't work properly?

Upvotes: 0

Views: 441

Answers (4)

Nicolás Torres
Nicolás Torres

Reputation: 1345

You are calling Ajax asynchronously therefore the system wont wait for ajax to end in order to continue proccessing. You'll have to add

async:false, 

To your ajax call, like this:

function checkFor(param){    
    $.ajax({
        url: "core/notifications.php",
        type: "POST",
        async:false,
        dataType: "json",
        data: {
            chk:param
        },
        success: function (data) { 
            if(data.status!="error")  {
                console.log(data);
               var ret=data;                
            }

        }
    });
return ret;
}

Hope it helps!

Upvotes: 0

Prasenjit Kumar Nag
Prasenjit Kumar Nag

Reputation: 13461

You can't return data from success callback. Instead you can call CheckAll from success callback like this

success: function (data) { 
            if(data.status!="error")  {
                console.log(data);
                //return data;                
                CheckAll(data);
            }

        }

To run checkFor instead every 10 seconds you can set the timer from within success callback too. That will call the checkFor 10 seconds after every successful ajax request. Using setInterval can end up with multiple simultaneous ajax calls.

success: function (data) { 
                if(data.status!="error")  {
                    console.log(data);
                    //return data;                
                    CheckAll(data);
                    setTimeout(checkFor,10000);
                }

            }

And your updated checkAll would be like

function CheckAll(data){

    if(mail_ntfy.attr("data-number")!=data.m_count && data.m_count!=0)
        mail_ntfy.attr("data-number", data.m_count);
    if(question_ntfy.attr("data-number")!=data.q_count && data.q_count!=0)
        question_ntfy.attr("data-number", data.q_count);
    if(users_ntfy.attr("data-number")!=data.u_count && data.u_count!=0)
        users_ntfy.attr("data-number", data.u-count);
    showNotes(data.msg);
    chngTitle(data.msg);    
}

Upvotes: 1

wroniasty
wroniasty

Reputation: 8052

If you want checkFor to return the data the AJAX call has to be synchronous. This is, however, bad Javascript practice (for example, it will hang the execution of scripts on the page until the request is complete). Unfortunately this whole design is flawed, but you could use this code if you REALLY have to:

function checkFor(param){    
  var result;
  $.ajax({
    url: "core/notifications.php",
    type: "POST",
    async: false,
    dataType: "json",
    data: {
        chk:param
    },
    success: function (data) { 
        if(data.status!="error")  {
            console.log(data);
            result = data;                
        }

    }
  });
  return result;
}

Upvotes: 1

lanzz
lanzz

Reputation: 43168

checkFor() does not return any result. The console.log() statement is in the anonymous function attached to the success handler of your AJAX request; its return does not return from the checkFor() function.

Upvotes: 1

Related Questions