在一些时候,希望一件task不能太快完成,需要大于多少时间才可以执行,就可以使用Promise的gtTime方法.

使用方式

        Promise.gtTime(f1(), 5000).then(f2);
        function f1() {
            var promise = Promise();
            setTimeout(function () {

                console.log(1);
                promise.resolve("from f1");
            }, 1500)

            return promise;
        }

        function f2() {
            var promise = Promise();
            setTimeout(function () {

                console.log(2);
                promise.resolve();
            }, 1500)

            return promise;
        }

f1执行只需要1.5秒,但是希望至少5秒后再执行f2,所以使用Promise.gtTime(f1(), 5000).then(f2)。如果f1需要8秒,则无视5秒的限制,8秒后才执行f2。

你可以点击这里下载最新版本。

有关promise更多信息你可以访问:

getting started with Promise.js(总)

相关文章:

  • 2021-05-30
  • 2021-12-04
  • 2022-12-23
  • 2021-07-25
  • 2021-06-16
  • 2021-07-05
  • 2021-05-15
  • 2021-07-14
猜你喜欢
  • 2021-12-27
  • 2021-10-11
  • 2021-06-23
  • 2021-06-14
  • 2021-09-05
  • 2021-12-05
  • 2021-12-04
相关资源
相似解决方案