简体   繁体   English

如何正确使用SetTimeout(打字稿)

[英]How to use SetTimeout correctly (typescript)

I am working on an ionic-cordova app that uses Geolocation . 我正在使用Geolocation的ionic-cordova应用程序上工作。

According to the app flow it is necessary to get an accurate position. 根据应用程序流程,有必要获得准确的位置。

I wrote dedicated methods to achieve the location and I used setTimeout in them. 我编写了专用的方法来实现该位置,并在其中使用了setTimeout

According to my logs it seems that I didn't use setTimeout correctly - or even worst I chose poorly and setTimeout is not what I need here. 根据我的日志,似乎我没有正确使用setTimeout甚至更糟的是我选择不当, setTimeout并不是我所需要的。

This is my code: 这是我的代码:

  getFixedLocation() {
    let coordinates: Coordinates;
    let maxTries = 15;
    let isFixedLocation = false;

    this.forceFixLocation(10);

    do {
      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 look on the log reveals that my do-while loop is executed before the first iteration of forceFixLocation : 在日志中查看可以发现我的do-while循环是在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): We are 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): We are 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 is the correct approach here? 这里正确的方法是什么?

The lambda function inside forceFixLocation will be called first time after 3 sec. 3秒钟后将首次调用forceFixLocation内部的lambda函数。 When you calling this.forceFixLocation(10); 当您调用this.forceFixLocation(10); from getFixedLocation - it immediate continues to next line and doing the logic of while. getFixedLocation它立即继续到下一行并执行while逻辑。 More than that, getFixedLocation may already return some value, but you will still get forceFixLocation called every 3 second (10 times). 不仅如此, getFixedLocation可能已经返回了一些值,但是您仍然会每3秒(10次)调用一次forceFixLocation

You are missing a fundamental information here. 您在这里缺少基本信息。 And it's the JavaScript event loop! 这就是JavaScript事件循环!

You can NOT use do while because it will never (ever) end, and will hang your application, as the logs indicate. 您不能使用do while因为它永远不会 (永远)结束,并且会挂起您的应用程序,如日志所示。

It also looks like you are not waiting for the promise to resolve.. the then method a promise provides is just a function that might/will happen in the future. 看起来您似乎并没有在等待诺言的解决。.诺言提供的then方法只是一个将来可能/将要发生的功能。 Your do/while hangs the app so it can't resolve to this promise... 您的do/while挂起了应用程序,因此它无法解决此诺言...

I would suggest trying async/await as it offers a similar syntax to what you are expecting to get out of JavaScript. 我建议尝试async / await,因为它提供了与您期望从JavaScript中获得的语​​法类似的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM