2012-06-23 34 views
0

我正在使用phonegap + sqlite。在sqlite中插入後window.location不能正常工作

我想插入一些東西。

這是我能夠插入數據的代碼 -

function insertDB() { 
     var sqlI = "INSERT INTO post_data(post_text,pdate) VALUES("Hello, world","23-05-2012"); 
     mydb.transaction(
          function(transaction) { 
          transaction.executeSql(sqlI, [], nullDataHandler, errorHandler); 
          }); 
     console.log("inserted"); 
     window.location="dashboard.html"; 
} 

當我刪除行window.location="dashboard.html"或當我設置1000毫秒的延遲,然後執行window.location="dashboard.html"。但上面的代碼不起作用。爲什麼?

+0

插入或重定向不起作用? – Sebas

+0

重定向工作。插入不。 –

回答

1

我認爲transaction函數是異步的。 嘗試並把重定向略高於:

mydb.transaction(
    function(transaction) { 
     transaction.executeSql(
      sqlI, 
      [], 
      function(transaction, results){ 
       // passing transaction and results in case you have to display some returned statements 
       window.location="dashboard.html"; 
      }, 
      nullDataHandler 
     ); 
}); 

正如你所看到的,重定向將在resultHandler處理。

+0

哇,這工作。我不知道那個交易功能是異步的。謝謝:) –

+0

是的,是的。剛纔在CAS中,我不清楚'function(transaction,results)'語句:如果你不處理結果,那麼你可以簡單地使用'function()',然後你可以獲得更好的性能。 – Zakaria