I am new to javascript, and can’t find the solution to this.
I have read some of the similar questions, but did not look like the problem was the same as mine.
I call a method from script1 with this code:
function turnPage(){
var current = window.now;
var nextpage = getNextPage(current);
alert(nextpage);
}
In script2 there is a SQLite etc:
function getNextPage(Pid) {
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM Page WHERE Pid=' + Pid, [],
function(tx, results) {
nextp = parseInt(results.rows.item(0).NextPage);
//alert(nextp);
return nextp;
}, errorCB);
}, errorCBQuery);
}
if I use the alert-dialog in the called function, the variable nextp is 2.
BUT if I return the variable, it will alert as ‘undefined’.
Also, if I etc make the variable var nextp = 11; over “db.transaction…” and the return-statement at the end of the function, it will return 11 instead of 2.
Is it because the variable is not sent to the inner function in my inception of functions? 🙂
Any ideas of what to do? thanks!
I don’t know how SQLite in javascript works, but I suspect it works asynchronously, so you’re calling
alertinturnPagebut the transaction is running async and the return value is in another scope anyway. You can try passing a callback function togetNextPageand then instead of returningnextpcall the callback withnextpas argument: