论坛首页 Web前端技术论坛

用Ext编排JavaScript任务

浏览 4634 次
精华帖 (0) :: 良好帖 (2) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-02-12  
《用Ext编排JavaScript任务Ext.TaskMgr Scheduling JavaScript Tasks》
By Aaron Conran
Ext支持版本: 1.x/2.0

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

    要编排一个任务,你可以按照以下的语句:
Ext.TaskMgr.start({run: myFunction, interval: 1000});

    除非关闭浏览器或调用stop方法停止任务,如果不是每一秒就会运行一次名为myFunction的函数。

任务的配置项:
* 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);

   发表时间:2008-02-13  
十分感谢,正在寻找这样的。

console.log 输出在什么地方??

0 请登录后投票
   发表时间:2008-02-13  
Kaki有安装Firebug吗?
F12弹出Firebug,输出在console的那个tab上
0 请登录后投票
   发表时间:2008-02-14  
什么场景下需要这样的功能,能否举几个具体的例子?
0 请登录后投票
   发表时间:2008-02-15  
在自动化测试的时候,可以试试
0 请登录后投票
   发表时间:2008-02-23  
备忘:EXT2.0的文档正式纳入Ext.TaskMgr了。
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics