如何在事务回调函数中传递参数 [英] how can I pass argument in transaction callback function
问题描述
从像这样的教程代码
function queryDB(tx) {
tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
}
function errorCB(err) {
alert("Error processing SQL: "+err.code);
}
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(queryDB, errorCB);
我想将一个变量作为参数传递给queryDB函数,所以我的代码想到应该看起来像
in db.transaction i want to pass a variable as argument to queryDB function, so the code which i think of should looks like
db.transaction(queryDB(id), errorCB);
我如何实际实现这个?或者它只是像这样工作,我的id将被传递并进入tx?
How I can actually implement this ? Or its simply gonna work like this and my id will be passed and get in tx ?
推荐答案
再次将它包裹在一个函数中
Wrap it in a function again
var id = 'THEID';
db.transaction(function(){
queryDB(id)
}, errorCB);
注意 - 假设您正在制作API。某些API /框架会自动插入所需的信息。例如
Note - This is assuming that you're making the API. Some APIs / frameworks insert the required information automatically. For example
//the db.transaction method
function transaction(param, callback) {
//do code stuff
callback(someInternalId); //callback is the function you pass as the first parameter
}
所以,如果你想要在回调中传递您自己的数据,将其包装在函数中。否则,您使用的代码可能会自动为您执行此操作。
So, if you want to pass your own data in the callback, wrap it in a function. Otherwise, the code you are using may be doing this for you automatically.
这篇关于如何在事务回调函数中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!