首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

贴一个解决js异步并行等候的代码

2012-12-24 
贴一个解决js异步并行等待的代码var Y YUI()var EventProxy function(){this.event_list {}this.d

贴一个解决js异步并行等待的代码

var Y = YUI();var EventProxy = function(){    this.event_list = {};    this.data_list = [];    this.callback = null;    this.trigger_flag = false;}EventProxy.prototype.assign = function(){    this.callback = Array.prototype.splice.call(arguments, arguments.length - 1, 1)[0];    if(typeof this.callback !== 'function'){        return false;    }    for(var i = 0,len = arguments.length; i < len; i++){        this.event_list[arguments[i]] = false;    }}EventProxy.prototype.trigger = function(event_name, data){    if(!this.callback || this.event_list[event_name]){        return false;    }    this.event_list[event_name] = true;    this.data_list.push(data);    this.trigger_flag = true;    for(var key in this.event_list){        if(this.event_list.hasOwnProperty(key) && !this.event_list[key]){            this.trigger_flag = false;        }    }    if(this.trigger_flag){        this.callback.apply(null, this.data_list);    }}var proxy = new EventProxy();proxy.assign('event1', 'event2', 'event3', function(v1, v2, v3){    Y.log(v1 + ' ' + v2 + ' ' + v3);})proxy.trigger('event1', 'data1');proxy.trigger('event2', 'data2');proxy.trigger('event3');
?

热点排行