开发者

How can I assign a call back function after I query something in javascript?

开发者 https://www.devze.com 2023-03-18 23:24 出处:网络
I typically, call this to query back the result: db.transaction(function(tx) { tx.executeSql(\'SELECT * FROM MyDB WHERE key = ?\', [aKey], MyDB.renderResults);

I typically, call this to query back the result:

db.transaction(function(tx) {
        tx.executeSql('SELECT * FROM MyDB WHERE key = ?', [aKey], MyDB.renderResults);
});

and the renderResults function is like that:

renderResults: function(tx, rs){
    // for(i = 0; i < rs.rows.length; i++){
    //      document.getElementById("textfield").value = "";
    //      document.getE开发者_C百科lementById("textarea").value += rs.rows.item(i).chiChar;
    // }
}

But I would like to make it more flexible, I would like to assign a function as a param to renderResults, in order to make my renderResult can execute a call back function. In other words,I would like to put the commented code into a function, which the renderResults only execute the function I put .....How can I do so ?? Thank you.


renderResults: function(tx, rs, callback){ 
    callback(tx, rs);        
}

function foo(tx, rs) {
    for(i = 0; i < rs.rows.length; i++){ 
        document.getElementById("textfield").value = "";
        document.getElementById("textarea").value += rs.rows.item(i).chiChar;
    } 
}

renderResults(tx, rs, foo);
0

精彩评论

暂无评论...
验证码 换一张
取 消