传递额外的参数到WebSQL回调函数? [英] Pass extra parameters to WebSQL callback function?
本文介绍了传递额外的参数到WebSQL回调函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的代码调用函数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 + "\nanotherVal: " + anotherVal);
}
(function() {
var anotherVal = "Whoa!",
anotherCallback = function(val) {
return myCallback(val, anotherVal);
};
doStuff(anotherCallback);
}());
这篇关于传递额外的参数到WebSQL回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文