LazyMan

      (function (win) {
        function _lazyMan (name) {
            var _this = this;

            this.name = name;
            this.queue = [];

            this.queue.push(() => {
                console.log('Init LazyMan ' + this.name);

                _this.next();
            });

            setTimeout(function () {
                _this.next();
            })
        }

        _lazyMan.prototype.next = function () {
            var fn = this.queue.shift();

            typeof fn === 'function' && fn(); 
        };

        _lazyMan.prototype.sleep = function(time) {
            var _this = this;

            this.queue.push(function () {
                setTimeout(function () {
                    console.log('LazyMan sleep ' + time + 's');
                    _this.next();
                }, time * 1000);
            });

            return this;
        };

        _lazyMan.prototype.run = function(str) {
            var _this = this;

            this.queue.push(function() {
                console.log('run ' + str);

                _this.next();
            });

            return this;
        }

        function LazyMan (name) {
            return new _lazyMan(name);
        }

        win.LazyMan = LazyMan;
    }(window));


    LazyMan('哈哈').sleep(4).run('test1').run('test2').run('test3');
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。