cordova - Cant return variable in recursive function javascript -
hi guys im creating phonegap program using javascript function return values colected in database , problem cant return values becouse in recursive function , everytime call main function doesnt retusn anything
the code is:
function exporta_imoveis(){ var db = window.opendatabase("ithomes", "1.0", "cordova demo", 200000); db.transaction(function coletandovisitas(tx){ alert('coletando imoveis no bd local '); tx.executesql('select * imoveis', [], function percorrendolinhas(tx, results){ var len = results.rows.length; var array_retorno = new array(); (var i=0; i<len; i++){ array_retorno[i] = results.rows.item(i); } return array_retorno; }, function err(){ alert('erro ao coletar') } ); },function erro(){ alert('erro ao coletar dados dos imoveis') }, function acerto(){ alert('dados dos imoveis coletadas com sucesso')} ); } i need value of array_retorno variable, please
the problem you're experiencing has nothing recursion asynchronous functions. function percorrendolinhas pass executesql executed when database action finished rest of mainfunction executed first. therefore should use callback:
function exporta_imoveis(callback){ var db = window.opendatabase("ithomes", "1.0", "cordova demo", 200000); db.transaction(function coletandovisitas(tx){ alert('coletando imoveis no bd local '); tx.executesql('select * imoveis', [], function percorrendolinhas(tx, results){ var len = results.rows.length; var array_retorno = new array(); (var i=0; i<len; i++){ array_retorno[i] = results.rows.item(i); } callback(array_retorno); }, function err(){ alert('erro ao coletar') } ); },function erro(){ alert('erro ao coletar dados dos imoveis') }, function acerto(){ alert('dados dos imoveis coletadas com sucesso')} ); } you can call function in following way:
exporta_imoveis(function (array_retorno) { //do array_retorno });
Comments
Post a Comment