1. function compose (fns ) {
    2. let i = -1;
    3. return function () {
    4. function run(index) {
    5. let fn = fns[index];
    6. if (typeof fn !== "function") {
    7. return;
    8. }
    9. return fn(run.bind(null,index+1));
    10. }
    11. return run(i+1);
    12. }
    13. }
    14. class TaskController {
    15. taskQueue = [];
    16. constructor() {
    17. setTimeout(() => {
    18. compose(this.taskQueue)();
    19. });
    20. }
    21. task(fn) {
    22. this.taskQueue.push(fn);
    23. return this;
    24. }
    25. // 多少毫秒以后执行下一个函数
    26. timeout(ms) {
    27. this.taskQueue.push(
    28. next => setTimeout(next, ms)
    29. );
    30. return this;
    31. }
    32. timeoutFirst(ms, fn) {
    33. this.taskQueue.push(
    34. next => fn(), setTimeout(next, ms)
    35. );
    36. return this;
    37. }
    38. }
    39. function builder() {
    40. return new TaskController();
    41. }