0

I'm trying to update information in all tables using ajax. All the tables I want to update have the same class. So I use .each to update them all:

setInterval(function(){
    $('.mtable').each(function(){
        console.log("call for " + this.id);
        mdebug(this.id);
        })
    },
    15000);

The function, that beeng called uses ajax to make a request to a server and uses received information to update its content.

function mdebug(apl_ip){
        table_id=apl_ip.replace(/\./g, "_");
        apl_ip=apl_ip.replace(/_/g, ".");
        var request=JSON.stringify({
            'host' : apl_ip,
            'method' : 'GET',
            'command': '/wga/reverseproxy/'
        });
        console.log("before ajax" + table_id);
        $.ajax({
            type : 'POST',
            url : 'isamreq.php',
            data : request,
            dataType : 'json',
            processData : false,
            contentType : 'application/json'
        })
        .done(function(serv_answ){
            console.log("after ajax" + table_id);
        });
}

To make explanation of the problem as simple as possible I left only two tables and only the code, nessesary to explanation.

Output console.log("before ajax" + table_id); shows ids of all the tables (since this call is made for all the tables). But console.log("after ajax" + table_id); is always the same! Here's the console output:

Console_output

So, I get

> before ajax val1 
> before ajax val2
> - ajax POST
> - ajax POST 
> after ajax val2 <<-- val1 IS LOST!
> after ajax val2 <<-┘

How is it possible that table_id is overidden and how to avoid it?

I would really appriciate your help!

  • it's called `closure` when the async operation is done the value already changed. i posted a question about it (more related to `var` vs `let`). maybe it can clear up some things. http://stackoverflow.com/questions/34554790/javascript-hoisting-var-vs-let – Sagiv b.g May 12 '17 at 15:00
  • I see that when the async operation is done the value already changed. But is it possible to make ajax really parallel to make the code work as desired? – Ivan Yartsev May 12 '17 at 15:49

0 Answers0