0

I am new to async functions. What I currently have is watchPosition(), which fires whenever a new position is detected. Well, what I need is to find a new speed every second. So, I want to call getCurrentPosition() every second, to record the speed of the user. I get a big backlog when I use setInterval() with getCurrentPosition().

What I want (time, speed):

1s, 1m/s
2s, 1.5m/s
3s, 1.2m/s...etc

What I get with setInterval and getCurrentPosition():

5s, 1m/s
5s, 1m/s
5s, 1m/s
10s, 1.5m/s
10s, 1.5m/s
10s, 1.5m/s...etc

With setInterval(), the speeds are all the same and I get a barrage of them every 5seconds or so. So I am confused, how should I approach this? Use an observable?


--edit: With watchPosition():

function getAverageWalkingSpeed():any {
    var options = {
        maximumAge: 1000,
        timeout: 5000,
        enableHighAccuracy : true,
    };
    let watch = navigator.geolocation.watchPosition(onSuccess, onError, options);
    
    function onSuccess(position: { timestamp: string | number | Date; coords: { speed: any; latitude: any; longitude: any; }; }) {
        let time = formatTime(new Date(new Date().getTime())); //time when speed was found
        let speed = position.coords.speed;
        console.log(time,speed)
    })
}

With a setInterval function, replacing previous watchPosition() with getCurrentPosition(). Should just need to call setTimer(true):

function setTimer(state: boolean){
    if(state){
        var timer = setInterval(getAverageWalkingSpeed, 1000);
    }
    else{
        clearInterval(timer)
    }
}
  • Have a look at this answer: https://stackoverflow.com/questions/5638783/how-do-i-get-this-javascript-to-run-every-second – Rylee Jul 24 '20 at 02:55
  • Does this answer your question? [How do i get this javascript to run every second?](https://stackoverflow.com/questions/5638783/how-do-i-get-this-javascript-to-run-every-second) – Rylee Jul 24 '20 at 02:57
  • added the code I am using. It does to an extent Rylee, but the code I am using backlogs because getCurrentPosition is async. So I get a barrage of speeds every 5 seconds, instead of a speed value every second like I am hoping for. –  Jul 24 '20 at 03:00

1 Answers1

1

You can create a wrapper function that awaits for getCurrentPosition, and then recursively calls itself after 1 second:

function mockGetCurrentPosition () {
  return new Promise((resolve,reject)=> {
    setTimeout(()=> resolve(console.log("x,y")),1000); 
  });
}


async function getPosition () {
  await mockGetCurrentPosition(); 
  setTimeout(()=> getPosition(), 1000); 
}

getPosition(); 
Pavlos Karalis
  • 2,893
  • 1
  • 6
  • 14
  • 1
    Appreciate it, this looks like what I was looking for. I will give it a shot and report back! –  Jul 24 '20 at 03:00
  • For some reason, when I use this and the geolocation api, it still does not give a speed every second. If you have any tips specifically with that api please let me know, otherwise I will use your async code as a foundation. Thank you! –  Jul 24 '20 at 06:39
  • It wouldn't be every second, it would be the time it takes to complete a call + 1 second; you can reduce the setTimeout duration, but if the async call takes over 1 second itself, there's no way to do it. – Pavlos Karalis Jul 24 '20 at 14:59
  • got it. WatchPosition() returns a value every second when outdoors (I am creating an indoor app, but I was testing to make sure I could get a value every second with a promise similar to how I can with WatchPosition() alone, and then apply that while indoors). But when I use a promise method, I could only get a value every 2-3s or so. Just odd, I guess I need to mess with it –  Jul 24 '20 at 18:06