Rumah > hujung hadapan web > tutorial js > JS异步函数队列功能及相关操作技巧

JS异步函数队列功能及相关操作技巧

小云云
Lepaskan: 2017-11-30 11:33:58
asal
1471 orang telah melayarinya

我们都知道JS能实现很多功能,本文我们将和大家分享如何实现JS异步函数队列功能及相关操作技巧。

场景:

做直播,会有入场消息,入场特效,用户如果有坐骑,需要给他展示几秒钟的坐骑特效,如果几个人同时进场,那该怎么展示呢?这时候就会想到setTimeout函数,对,思路不错,但是,异步函数队列怎么实现呢?直接上代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

var Queue = function() {

    this.list = [];

};

Queue.prototype = {

    constructor: Queue,

    queue: function(fn) {

      this.list.push(fn)

      return this;

    },

    wait: function(ms) {

      this.list.push(ms)

      return this;

    },

    dequeue: function() {

      var self = this,

        list = self.list;

      self.isdequeue = true;

      var el = list.shift() || function() {};

      if (typeof el == "number") {

        setTimeout(function() {

          self.dequeue();

        }, el);

      } else if (typeof el == "function") {

        el.call(this)

        if (list.length) {

          self.dequeue();

        } else {

          self.isdequeue = false;

        }

      }

    }

};

Salin selepas log masuk

例子:

如果a,b差不多同时进来;
c在a,b还没完全出队列的时候,进来的;
d在a,b,c都除了队列之后再进来的。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

var q = new Queue();

function a() {

    console.log("a执行了", new Date());

}

function b() {

    console.log("b执行了", new Date());

}

function c() {

    console.log("c执行了", new Date());

}

function d() {

    console.log("d执行了", new Date());

}

q.wait(2000);

q.queue(a);

q.wait(2000);

q.queue(b);

q.dequeue();

setTimeout(function(){//3S之后进来的

    q.wait(2000);

    q.queue(c);

},3000);

setTimeout(function(){//8S之后进来的

    q.wait(2000);

    q.queue(d);

    q.dequeue();

},8000);

Salin selepas log masuk

这里我们就需要判断什么时候要调用dequeue来出队列了。(为什么c进队列的时候,不需要dequeue,但是d进来的时候就需要dequeue了呢?)

但是一般我们是无法知道用户什么时候进场的,一般都是进队列了,就该能出队列,但是如果用户是在空队列的时候进来的,之前的递归调用dequeue就执行完了,你进来需要再执行 出队列的操作。

于是,代码应该这样:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

var q = new Queue();

  function a() {

    console.log("a执行了", new Date());

  }

  function b() {

    console.log("b执行了", new Date());

  }

  function c() {

    console.log("c执行了", new Date());

  }

  function d() {

    console.log("d执行了", new Date());

  }

  q.wait(2000);

  q.queue(a);

  if (!q.isdequeue) {

    q.dequeue();

  }

  q.wait(2000);

  q.queue(b);

  if (!q.isdequeue) {

    q.dequeue();

  }

  setTimeout(function() { //3S之后进来的

    q.wait(2000);

    q.queue(c);

    if (!q.isdequeue) {

      q.dequeue();

    }

  }, 3000);

  setTimeout(function() { //8S之后进来的

    q.wait(2000);

    q.queue(d);

    if (!q.isdequeue) {

      q.dequeue();

    }

  }, 8000);

Salin selepas log masuk

这样,每进一次队列,就判断要不要出队列,事情就OK了。

以上内容就是如何实现JS异步函数队列功能及相关操作技巧,希望对大家有帮助。

相关推荐:

异步函数如何使用?总结异步函数实例用法

获取JavaScript异步函数的返回值实例详解

JavaScript异步函数返回值的获取方法

Atas ialah kandungan terperinci JS异步函数队列功能及相关操作技巧. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan