0

I am in mobile app. I use ajax calls to receive data from webserver with this code:

            $.ajax({
                url: 'http://www.xxxxxxxxxxxx',
                data: {name: 'Chad'},
                dataType: 'jsonp',
                success: function(data){
                    $.each(data.posts, function(i,post){
                        $.mobile.notesdb.transaction(function(t) {
                        t.executeSql('INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount, pastpayments, todaypayments, receiptno) VALUES (?,?,?,?,?,?,?,?,?,?,?);',
                            [post.Id, post.Code, post.Address, post.Name, post.Description, post.EntrySeason, post.Period, post.Revenue, post.PastPayments, post.todaypayments, post.receiptno],
                            function(){ 
                                bill = 1;
                                $('#mycontent').append("bill - OK");
                            }
                        );
                        });             
                    });
                }
            });

I want bill - OK displayed only once after all data inserted into sqlite.

3
  • And your actual problem is...? Commented Feb 22, 2012 at 20:50
  • bill - OK displayed for every insert to sqlite post... 20 times Commented Feb 22, 2012 at 20:51
  • Move your "bill - OK" line outside of the $.each loop. Commented Feb 22, 2012 at 21:01

2 Answers 2

1

try this:

success: function(data){

    var count = data.posts.length;

    $.each(data.posts, function(i,post){
        $.mobile.notesdb.transaction(function(t) {
            t.executeSql(<...>,
                function() { 
                    bill = 1;
                    if (--count == 0) {
                        $('#mycontent').append("bill - OK");
                    }                        
                }
            );
        });             
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

try to add:

$.ajax({
    url: 'http://www.xxxxxxxxxxxx',
    data: {name: 'Chad'},
    dataType: 'jsonp',
    async: false, //this line here
    //...

EDIT

OK, what about:

$.each(data.posts, function(i,post){
    //rest of stuff
});

$('#mycontent').append("bill - OK"); //this line outside the each() call

2 Comments

this way bill ok displed once but before insertion complete
along with asynch: false line?

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.