如何访问executeSql函数之外的url
变量? (评论我想要的地方)
function generateUrl(id, callBack){
var db = window.openDatabase("Database", "1.0", "Marktplaats", 200000);
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM searches, settings', [], function (tx, results) {
var url = "http://google.nl";
}, null);
//I want to access the url variable here
});
}
答案 0 :(得分:1)
你建议的内容没有意义,因为executeSql
是异步的。
tx.executeSql('SELECT * FROM searches, settings', [], function (tx, results) {
var url = "http://google.nl";
// This is executed 2nd
}, null);
//This is executed 1st
});
因此,在您发表评论时,url
无法获得价值。
您应该做的是使用callBack
参数将url
传递给调用者:
tx.executeSql('SELECT * FROM searches, settings', [], function (tx, results) {
var url = "http://google.nl";
callBack.call(this, url);
}, null);
然后您可以将generateUrl
称为:
generateUrl(15, function(url) {
// URL is visible now
console.log(url);
});