Ext.TaskMgr是ExtJS库中一项未归档的功能,允许以可编程的方式编排调度某项任务。你可反复每隔一定时间地运行,也可以指定每个任务运行的次数、运行的持续时间和运行的频率等等。Ext.TaskMgr其实是Ext.TaskRunner的一个实例,它的源码可以source/util/TaskMgr.js找到。
要编排一个任务,你可以按照以下的语句:

Ext.TaskMgr.start(...{run: myFunction, interval: 1000}); 任务的配置项:
* run: 编排的函数
* scope: 执行的作用域
* interval: 运行的频率
* duration: 运行多久
* args: 要传入到编排函数内的参数,缺省下函数所接受到的参数为你任务已运行的次数
* repeat: 任务运行的次数
值得注意的是,如果你安排了一个间隔时间500ms的任务,它运行10次后所花的时间并非一定绝对是5000ms,可能有少少误差,如果要避免这种误差,你应配置repeat项代替duration。
TaskMgr对象并没有自带的延时执行任务功能,不同我们可以通过方法defer来到达推迟(延时)任务的目的:

Ext.TaskMgr.start.defer(4000, this, [...{run: this.myFunction, interval: 1000, scope: this}]); 
var UtilityClass = function() ...{ 
return ...{ 
myOtherTask: function(val) ...{
console.log('running in a different class: ' + val)
}
};
};

var TaskMgrTest = function() ...{ 
return ...{ 
init: function() ...{
var util = new UtilityClass(); 
/**//* run this.myTask every 5000ms in the scope of this */ 
Ext.TaskMgr.start(...{run: this.myTask, interval: 5000, scope: this});

/**//* run util.myOtherTask every 500ms for 5000ms in the scope of util */ 
/**//* override the default argument by a passed in array */ 
Ext.TaskMgr.start(...{run: util.myOtherTask, interval: 500, scope: util, args: ['overriden value'], duration: 5000});

/**//* run this.theLastOne every 1000ms 10x in the scope of this */ 
/**//* defer (delay) the scheduling of the task for 4000ms */ 
Ext.TaskMgr.start.defer(4000, this, [...{run: this.theLastOne, interval: 1000, repeat: 10, scope: this}]);
}, 
myTask: function() ...{
console.log('hola ' + new Date().getTime());
}, 
/**//* by default we are passed an argument of numTimesRun */ 
/**//* which keeps track of how many times this task has run */ 
theLastOne: function(numTimesRun) ...{
console.log('only run ' + numTimesRun + '/10');
}
};
}();
Ext.EventManager.onDocumentReady(TaskMgrTest.init, TaskMgrTest);
本文介绍ExtJS库中未归档的Ext.TaskMgr功能,详细解释如何通过编程方式编排任务,包括设置运行频率、持续时间及作用域等,并提供具体示例。
36

被折叠的 条评论
为什么被折叠?



