angck
3/12/2014 - 3:22 AM

javascript 异步编程

javascript 异步编程

function Queue(){
    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 = this.list, el = list.shift() || function() {};
        if(typeof el == 'number') {
            setTimeout(function(){
                list.length && self.dequeue();
            }, el);
        } else if(typeof el == 'function') {
            el.call(this);
            list.length && self.dequeue();
        }
    }
};