ExtJs 学习笔记(2)TaskRunner

主要两项功能:执行次数的限制、执行时间的限制(即超时)。TaskRunner 旨在利用计时器分时执行方法提供一个相对简易的并行运行机制,其目的不但在于一般的延时执行任务,还可以同时进行多项任务。这样的话,任意个独立的任务都可以在任何时候开始,并彼此独立地运行。那么是否与多线程的概念有些相近呢?其实不尽然——这里必须说明一下,尽管 ExtJS 官方文档提出“提供以多线程的方式执行一个或多个任务的能力”的概念,但并不是真正的代替“线程(thread)”,仅仅是“模拟”。因为我们晓得,浏览器接口也好,JS运行时也好,仍不出“单线程模型”的范围内,所以我们认为充其量只是模拟线程的一种手段。在内部结构中,由数组 tasks[] 保持着任务对象的队列。


能提供多线程的定时服务,该类提供了多线程任务的管理。你可以通过Ext.TaskMgr来创建TaskRunner的一个实例,其次也可以自行创建Ext.util.TaskRunner的实例。该类可保障每一个任务或服务都可以在任何时刻独立的运行,而不会影响其他的任务或服务的运行。

如下一个简单实例:

var task = 
{
	run: function() 
	{
		Ext.fly().update(new Date().format('g:i:s A'));
	},
	interval: 1000 //单位毫秒
}

var runner = new Ext.util.TaskRunner();

runner.start(task);

(1)构造函数
构造一个函数,通过参数指定创建一个定时任务
TaskRunner([interval])
interval:可选参数,当新建立一个定时任务时,可指定interval毫秒作为该线程执行的时间间隔,如果不选该参数默认指定为10毫秒
(2)启动一个线程start()
start(task)
task:
可选参数,如果task不为空,必须具备如下属性:
run:一个函数来指定任务在某个时间执行,执行的作用域必须指明。
interval:指定任务连续执行的时间间隔。
scope:task对象run函数的作用域。
duration:在执行线程前先停止的时间。
repeat:重复执行的次数。
(3)停止一个线程stop()
stop(task)
停止指定的任务线程。
(4)停止所有线程stopAll()
stopAll()
停止所有的任务线程。



应用实例:

var task = {
	run:function(){
		Ext.Msg.alert("提示","线程已经运行!我将每次运行重复5次!但10秒后我将停止!");
	},
	interval:1000,
	repeat:5,
	duration:10000
};

var runner = new Ext.util.TaskRunner();

runner.start(task);

runner.stopAll();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Book Description A hands-on approach to mastering the fundamentals of Grunt Overview Gain insight on the core concepts of Grunt, Node.js and npm to get started with Grunt. Learn how to install, configure, run, and customize Grunt Example-driven and filled with tips to help you create custom Grunt tasks In Detail In recent times, modern web browsers have become the application platform of choice. Grunt, along with its wide range of plugins, provides a simple way of managing the large number of build tasks required to maintain a sophisticated web application. Getting Started with Grunt: The JavaScript Task Runner provides you with all the information you need to become an effective Grunt power-user. You will quickly learn how to install, configure, and run Grunt. You will go on to understand how to use third-party Grunt and then create your own Grunt tasks that cater to your particular needs. This book first demonstrates various Grunt use cases before running through the steps of installing, configuring, running, and customizing Grunt. You will learn how to install Node.js, the Node.js package manager (npm), and Grunt. Then, you will understand how to set up and configure a personalized Grunt environment. Next, you will look at the various methods of running and customizing Grunt to utilize its flexibility. Finally, to emphasise what has been learnt, you will see a complete example build of a web application. Getting Started with Grunt: The JavaScript Task Runner will enable you to create your very own Grunt environments from scratch and fully utilize Grunt's large feature set to effectively solve your custom requirements. What you will learn from this book Learn about Grunt and its advantages Understand Node.js and how it relates to Grunt Take an in-depth look at npm, Node.js modules, and the working of Grunt plugins Get familiar with installing Grunt and setting up your first Grunt build environment Gain insight on the methods of configuring Grunt and when each method should be used Effectively execute Grunt through the use of task arguments, task aliasing, multi-task targets, and more Construct your own Grunt tasks, multi-tasks, and asynchronous tasks Approach A step-by-step, practical tutorial to help you transform into a Grunt power-user. Who this book is written for This book is for JavaScript developers who want to get to grips with GruntJS and use it to build and test their JavaScript applications. The only requirement for this book is a basic understanding of objects and functions in JavaScript. Product Details Paperback: 132 pages Publisher: Packt Publishing (February 19, 2014) Language: English ISBN-10: 1783980621 ISBN-13: 978-1783980628 Product Dimensions: 9.2 x 7.5 x 0.3 inches
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值