2012-02-22 3 views
0

모바일 앱입니다. 나는 아약스는이 코드를 웹 서버로부터 데이터를 수신 호출을 사용완료 후 jquery ajax call once once 함수

  $.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"); 
          } 
         ); 
         });    
        }); 
       } 
      }); 

내가 bill - OK SQLite는에 삽입 된 모든 데이터 후 한 번만 표시합니다.

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

편집 대해

확인 :

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

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

을 그리고 실제 문제는 .. .? –

+0

청구서 - sqlite 게시에 대한 모든 삽입물에 대해 표시되는 확인 ... 20 회 – kosbou

+0

'$ .each'루프 외부로 'bill - OK'행 이동. – SenorAmor

답변

0

은 추가하려고

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"); 
        }       
       } 
      ); 
     });    
    }); 
} 
+0

아니 작동하지 않습니다 – kosbou

+0

이 방법 청구서는 한 번만 삽입되기 전에 완료 – kosbou

+0

와 함께 asynch : false line? – aletzo