在Javascript中用command模式模拟多线程,包含注释
2006-12-25 09:45
411 查看
Javascript本身是不支持多线程的,直到在网上看到这样一篇文章, javascript中用command模式模拟多线程中 ,觉得非常好,用command模式,巧妙的模拟了多线程.虽是模拟,但很实用.对于javascript不熟的人,可能很难去理解,因此添加了注释,重新发布出来。希望能给需要的人一点帮助。
<html>
<head>
<title>用command模式模拟多线程</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
//objectName.prototype:返回Array对象类型原型的引用
//命令出队列,返回第一个元素
if (Array.prototype.shift==null)
Array.prototype.shift = function ()
//命令进队列,返回长度
if (Array.prototype.push==null)
Array.prototype.push = function ()
//为一个ArrayList对象,其属性shift将返回队列中的第一个元素,即一个函数对象.
var commandList = [];
//控制每次运行多少个动作
var nAction = 0;
//object.constructor表示创建对象的函数
//用于增加线程。克隆一个div对象,添加到页面,并且进站simThread命令
function startNewTask()
//模拟线程
function simThread(temp,n)
window.onload = function()
//让多个函数可以一起执行的方法。
function executeCommands()
//-->
</script>
<button onclick="startNewTask()">开始新线程</button>
<br/>
<div id="sampleResult" onmouseover="this.stop=true" onmouseout="this.stop=false" style="display:none;cursor:hand">0</div>
</body>
</html>
上面值得说明的是几个对象:
一,CommandList为一个ArrayList对象,可以通过ArrayList.shift()取出第一个元素,可通过ArrayList.push()将元素放进去.其ArrayList中保存的对象为Command.
二,Command对象为一个函数如下,可以直接执行.
function (){simThread(resultTemp,n);}其中n是可以传递的变量
三,command.constructor即FunctionConstructor
function Function()
{
[native code]
}
<html>
<head>
<title>用command模式模拟多线程</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
//objectName.prototype:返回Array对象类型原型的引用
//命令出队列,返回第一个元素
if (Array.prototype.shift==null)
Array.prototype.shift = function ()
//命令进队列,返回长度
if (Array.prototype.push==null)
Array.prototype.push = function ()
//为一个ArrayList对象,其属性shift将返回队列中的第一个元素,即一个函数对象.
var commandList = [];
//控制每次运行多少个动作
var nAction = 0;
//object.constructor表示创建对象的函数
//用于增加线程。克隆一个div对象,添加到页面,并且进站simThread命令
function startNewTask()
//模拟线程
function simThread(temp,n)
window.onload = function()
//让多个函数可以一起执行的方法。
function executeCommands()
//-->
</script>
<button onclick="startNewTask()">开始新线程</button>
<br/>
<div id="sampleResult" onmouseover="this.stop=true" onmouseout="this.stop=false" style="display:none;cursor:hand">0</div>
</body>
</html>
上面值得说明的是几个对象:
一,CommandList为一个ArrayList对象,可以通过ArrayList.shift()取出第一个元素,可通过ArrayList.push()将元素放进去.其ArrayList中保存的对象为Command.
二,Command对象为一个函数如下,可以直接执行.
function (){simThread(resultTemp,n);}其中n是可以传递的变量
三,command.constructor即FunctionConstructor
function Function()
{
[native code]
}
相关文章推荐
- 在Javascript中用command模式模拟多线程,包含注释
- 在javascript中用command模式模拟多线程
- JavaScript用Command模式模拟多线程
- JavaScript用Command模式模拟多线程
- 用fason的参数化定时器在javascript中模拟多线程
- 用javascript模拟多线程
- Javascript模拟多线程
- Javascript模拟多线程
- 关于使用Javascript来模拟多线程的一点想法
- c# 多线程模拟生产者消费者
- 多线程:模拟循环运行
- JavaScript 注释 变量
- (面试)多线程模拟火车站窗口售票程序。
- javascript自动生成包含数字与字符的随机字符串
- 多线程模拟生产者和消费者模型
- 多线程模拟实现生产者/消费者模型
- PHP 异步执行方法,模拟多线程的应用分析
- HTML、CSS、Javascript的注释
- JavaScript 用DIV模拟弹出窗口并跟随窗体滚动
- JavaScript中通过闭包解决只能取得包含函数中任何变量最后一个值的问题