0

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?

4 Answers 4

1

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.

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

Comments

1

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;
}

Comments

1

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);    
}

2 Comments

@epic_syntax: You cant. It's aynchronous.
@epic_syntax: you don't return. But you can do things in CPS style, just like the AJAX request itself is. Have your function receive another function to call when its done and call that function from inside the AJAX callback, passing the "return value" as an argument to it.
0

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!

1 Comment

this is wrong, it will not return the data to the caller of checkFor

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.