문제

i am testing Phonegap Web Storage for a small project:

var listsCount = 0;

tx.executeSql("SELECT * FROM list WHERE id = ?", [id], successGetList, errorGetList );

function successGetList(tx, results){
    listsCount = results.rows.length; // this will be 2, in my case
}

function errorGetList(err){
    console.log("Error processing SQL: "+err.code);
}

console.log(listsCount); // this will show 0, instead of 2

the issue i'm having is that listsCount doesn't get set inside the successGetList method. Even if i return it there.

any ideas on how can i set that variable inside of successGetList function ?

thanks

도움이 되었습니까?

해결책

It does get set, but your console.log call is being fired before that (that's how asynchronous code works!). Just use the value from the callback. If you need, call another function from there, and pass your data to it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top