$.when.apply(null, table).done(callback);这个是jquery里的吗?今天看到一个网上的例子,发现table=[]是个数组,用上$.when.apply就可以监听完成后执行callback 方法callback就是一个function();--------------------------------我问题是,怎么处理给一个数组监听,上面的例子数组操作完成执行callback,怎么实现的呢?

解决方案 »

  1.   


    把数组的方法再包一层push什么的执行完了再调一次回调函数
      

  2.   

    Array.prototype.add = function(item){
        this.push(item);
        if(this.callback) this.callback.call(this,item);
    }然后你写数组,添加成员时不用push,使用addvar arr = new Array();
    arr.callback = function(item){alert(item);}arr.add('1');
    arr.add('2');
    arr.add('a');
      

  3.   

    这里以push元素为例写个,需要的话自己举一反三。Array.prototype._push = Array.prototype.push;
    Array.prototype.push = function(v){
        this._push(v);
        if(typeof this.pushListener == 'function') this.pushListener.call(this,v)
    }
    var a = [], b = [];
    a.pushListener = function(v){
        alert('在数组a中添加新元素'+v);
    }
    a.push(1);b.pushListener = function(v){
        alert('在数组b中添加新元素'+v);
    }
    b.push(1);