어떻게 웹 sql 의 비동기 문 제 를 해결 합 니까?

1733 단어 Html5WebSql
1. 우선 websql openDatabase () 방법 으로 데이터 베 이 스 를 여 는 결 과 는 반드시 비동기 방식 으로 결 과 를 되 돌려 줍 니 다. openDatabaseSync () 방법 은 데이터 베 이 스 를 동기 화 할 수 있 지만 safari 유람 기 에서 이런 방법 을 찾 을 수 없 을 것 같 습 니 다.
원문:http://stackoverflow.com/questions/4052479/html5-database-api-synchronous-request
To get an object implementing  DatabaseSync  you have to call  openDatabaseSync(...)  instead of openDatabase(...) . I don't know about the iPhone, or what the  oDB  object you have is, but according to spec you only get the  openDatabaseSync  method in a WebWorker and not in the normal web browser  window . Certainly  XMLHttpRequest  has demonstrated that potentially-length synchronous operations in the UI thread are not a good idea.
 
2. 외국인 의 또 다른 방안 을 살 펴 보 았 다.http://stackoverflow.com/questions/3903155/synchronous-query-to-web-sql-database
사실 리 셋 함수 에 직접 쓸 수 있 음 을 발견 하면 여러 함수 로 나 눌 필요 가 없습니다. 그러면 비동기 문 제 를 해결 할 수 있 습 니 다. 다음 과 같 습 니 다. (모든 코드 논 리 는 리 셋 함수 에서 이 루어 집 니 다)
 
functiongetFolder(id, callback) {
          vardata = [];
          ldb.transaction(function(tx) {
          tx.executeSql('SELECT * FROM folders where id=?',
              [id],
            function(tx, results) {
                if(results.rows && results.rows.length) {
                    for(i = 0; i < results.rows.length; i++) {
                        data.push(results.rows.item(i));
                    }
                }
                if( typeof(callback) == 'function') callback(data);
          },
          function(tx, error) {
              console.log(error);
          });
      });

좋은 웹페이지 즐겨찾기