javascriptajaxjqmobiappframework

Interactive for + get function


I need a problem with a function to update some informations in my database (I'm using the websql). In my application, I'm showing a loading box for do the update, but, the for ends before the get function is complete and the loading box e closed.

I need that the next interaction of the for only happen when the get is finish.

Someone has a solution for this? :(

I'm using the appframework intel (jqmobi)

for (var i=0; i<len; i++){
    var url = "http://172.25.129.30:8180/prax/rest/ocorrenciaJSONBean/consultarStatusOcorrencia/"+results.rows.item(i).id;
    $.get(url,function(data){
        tx.executeSql('UPDATE ocorrencias SET status="'+data.responseText+'" WHERE id = '+results.rows.item(i).id);
    });
};

I have the following scenario:

An internal bank with a list of calls that a user opened on cell. What I want is to upgrade the status of the bank.

Today I do a query on internal bank and each id I perform a get on a webservice place to know the status of the call. I would like the next interaction is only occurred when the GET was finalized.

The problem here is that once the function ends, the screen refreshes and the message is "loading" disappears. This is occurring earlier than expected, since the end is before all GETs are finalized.


Solution

  • The get is not waiting to perform a call so you need to do a callback in your get function to perform the next one. You could do something like this:

    var index = -1;
    function perform_get(){
        index++;
        var url = "http://172.25.129.30:8180/prax/rest/ocorrenciaJSONBean/consultarStatusOcorrencia/"+results.rows.item(index).id;
        $.get(url,function(data){
            tx.executeSql('UPDATE ocorrencias SET status="'+data.responseText+'" WHERE id = '+results.rows.item(index).id);
        })
        .done(perform_get);
    }
    

    Now it is waiting to perform the next get call.

    Hope this helps.