javascript - How to use SetTimeout correctly (typescript) -


i working on ionic-cordova app uses geolocation.

according app flow necessary accurate position.

i wrote dedicated methods achieve location , used settimeout in them.

according logs seems didn't use settimeout correctly - or worst chose poorly , settimeout not need here.

this code:

  getfixedlocation() {     let coordinates: coordinates;     let maxtries = 15;     let isfixedlocation = false;      this.forcefixlocation(10);      {       console.log("getting location, try #" + maxtries)       this.geolocation.getcurrentposition(geolocation_options)       .then((position) => {         coordinates = position.coords;       });        if (coordinates.accuracy < 25 && coordinates.speed < 1) {         isfixedlocation = true;       } else {         maxtries -= 1;       }     } while(maxtries > 0 && !isfixedlocation);      if (isfixedlocation) {       return coordinates;     } else {       return null;     }   }    forcefixlocation(counter: number) {     settimeout(() => {       if(counter > 0) {         console.log("forcing location #" + counter)                 this.geolocation.getcurrentposition(geolocation_options)         .then((position) => {           console.log("got position, accuracy: " + position.coords.accuracy)         });          this.forcefixlocation(counter - 1);       }     }, 3000);   } 

a on log reveals do-while loop executed before first iteration of forcefixlocation:

08-15 00:58:51.831 d/systemwebchromeclient(19622): file:///android_asset/www/build/main.js: line 56358 : getting location, try #15 08-15 00:58:51.831 i/chromium(19622): [info:console(56358)] "getting location, try #15", source: file:///android_asset/www/build/main.js (56358) 08-15 00:58:51.831 d/geolocationplugin(19622): entering execute 08-15 00:58:51.841 d/systemwebchromeclient(19622): file:///android_asset/www/build/main.js: line 1362 : error 08-15 00:58:51.841 i/chromium(19622): [info:console(1362)] "error", source: file:///android_asset/www/build/main.js (1362) 08-15 00:58:54.834 d/systemwebchromeclient(19622): file:///android_asset/www/build/main.js: line 56381 : forcing location #10 08-15 00:58:54.844 i/chromium(19622): [info:console(56381)] "forcing location #10", source: file:///android_asset/www/build/main.js (56381) 08-15 00:58:54.844 d/geolocationplugin(19622): entering execute 08-15 00:58:56.936 d/systemwebchromeclient(19622): file:///android_asset/www/build/main.js: line 1362 : error 08-15 00:58:56.946 i/chromium(19622): [info:console(1362)] "error", source: file:///android_asset/www/build/main.js (1362) 08-15 00:58:57.847 d/systemwebchromeclient(19622): file:///android_asset/www/build/main.js: line 56381 : forcing location #9 08-15 00:58:57.847 i/chromium(19622): [info:console(56381)] "forcing location #9", source: file:///android_asset/www/build/main.js (56381) 

what correct approach here?

the lambda function inside forcefixlocation called first time after 3 sec. when calling this.forcefixlocation(10); getfixedlocation - immediate continues next line , doing logic of while. more that, getfixedlocation may return value, still forcefixlocation called every 3 second (10 times).


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -