js定时器高级用法

 

 

//////////////////////////////////////////////批量执行方法start
var delegateArray = [];
var DeArr = {
    AddFunc: function (func, time) {
        if (time == undefined || time == null || time == '') {
            time = 0;
        }
        var obj = {
            func: func,
            time: Number(time)
        };
        this.FuncList.push(obj);
        return this;
    },
    FuncList: [],
    Init: function () {
        delegateArray.push(this.FuncList);
        this.FuncList = [];
        return delegateArray.length - 1;
    },
    Execute: function (index) {
        if (index < 0) {
            return;
        } else {
            var o_funcList = delegateArray[index];
            NoAsyncFunc(o_funcList, 0);
        }
    }
}
function NoAsyncFunc(funcList, index) {
    if (funcList.length > index) {
        setTimeout(function () {
            funcList[index].func();
            index++;
            if (funcList.length > index) {
                NoAsyncFunc(funcList, index);
            }
        }, funcList[index].time);
    }
}
//用法示例
    //var index = DeArr.AddFunc(function () { console.log(1); 
    //                                      }).AddFunc(function () { console.log(2);
    //                                                              }, 2000).AddFunc(function () { console.log(3); 
    //                                                                                            }, 2000).Init();
    //DeArr.Execute(index);
//////////////////////////////////////////////批量执行方法end


转载请注明出处,谢谢。

 

posted @ 2021-05-31 14:51  木板  阅读(99)  评论(0)    收藏  举报