1、js脚本队列实现
function SubmitTaskList() { var obj = {}; var taskList = []; var index = 0; obj.AddTask = function (task) { if (task) taskList.push(task); } obj.Post = function () { if (index < taskList.length) { var task = taskList[index++]; if (!task.Check()) { return; } task.onSuccess = function () { obj.Post() }; task.Post(); } else obj.Rest(); } obj.Check = function () { for (var i = 0; i < taskList.length; i++) { task = taskList[0]; if (!task.Check()) { return false; } } return true; } obj.Rest = function () { index = 0; } return obj; } 执行对象 function SubmitTask(checkFun, postFun) { var obj = {}; obj.Check = checkFun; obj.Post = function () { postFun(obj.onSuccess); } return obj; }
2、使用
var taskList = SubmitTaskList(); 声明方法对象 taskList.AddTask(SubmitTask(Submit_Check, submitContactDifferInfo));添加执行队列 taskList.AddTask(SubmitTask(Submit_Check, function (onSuccess) { alert("保存成功"); if (onSuccess) { onSuccess(); } })); taskList.Post();