问题描述
我正在使用以下代码调用函数 db.transaction:
I am calling a function db.transaction with following code:
db.transaction(createSheetDB, function(){alert("Sheet creation error!")}, function(){alert("Sheet created!")});
函数 createSheetDB 是一个回调函数,它被 db.transaction() 隐式调用,它也传递一个参数 tx.我已经实现了这样的函数 createSheetDB(tx):
The function createSheetDB is a callback function which is implicitly called by db.transaction() which also passes it a parameter tx. I have implemented function createSheetDB(tx) like this:
function createSheetDB(tx) {
var nextId = getNextId();
tx.executeSql("INSERT INTO SHEET(id, name, desc) VALUES("+nextId+",'"+sheetName+"','"+desc+"')", [],
function(){alert("Sheet row inserted!")},
function(tx, err){alert("Sheet row insertion Error: "+err.message+" "+err.code)}
);}
现在的问题是 sheetName 和 desc 的值仅在调用函数中可用.我如何将它们传递给函数 createSheetDB(tx)?
Now the problem is the values of sheetName and desc are available only in the calling function. How do I pass them onto function createSheetDB(tx)?
推荐答案
您可以使用一种技术来创建一个新回调,该回调将关闭您想要的变量.
You can use a technique whereby you create a new callback that will close over the variables you want.
function doStuff(callback) {
var val = 43;
callback(val);
}
function myCallback(val, anotherVal) {
alert("val: " + val + "
anotherVal: " + anotherVal);
}
(function() {
var anotherVal = "Whoa!",
anotherCallback = function(val) {
return myCallback(val, anotherVal);
};
doStuff(anotherCallback);
}());
这篇关于将额外的参数传递给 WebSQL 回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!