HTML5 Database API : Synchronous request

后端 未结 3 582
一个人的身影
一个人的身影 2021-01-12 11:21

i currently use the client side database on an html5 iphone webapp. In my code i need to check if a row is present in the local DB :

function isStarted(oDB         


        
3条回答
  •  时光说笑
    2021-01-12 12:02

    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.

    It's not possible to run asynchronous code synchronously, or vice versa. To do so you'd need language-level features like threads or co-routines that JavaScript doesn't have. You have to exit your functions and return control to the browser to allow it to perform the HTTP request or database query, and call you back on the handler function you gave it.

    So you will have to rewrite your code ‘inside-out’ to pass callback functions instead of expecting return values, every time you do something involving database IO.

    function tellMeWhenIsStarted(oDB, callback) {
        oDB.query(sql,params,function(transaction,result) {
            callback(result.rows.length>0);
        }
    });
    

提交回复
热议问题