JavaScript用Command模式模拟多线程
2008-05-20 13:09
417 查看
以前我们解决这样的问题都是针对具体问题写一段代码来模拟多线程的,但是由于往往要对没个线程单独编码,这样的代码十分冗长。学习设计模式的时候就曾经考虑过在javascript中实用command模式来更好的模拟多线程,但是一直没有付诸实施,今天既然想起来了就试试看:
1<html><head><title>用command模式模拟多线程</title></head><body>
2</SCRIPT>
57<button onclick="startNewTask()">开始新线程</button>
58
59<BR><BR>
60<div id=sampleResult onmouseover="this.stop=true" onmouseout="this.stop=false" style="display:none;cursor:hand">0</div>
61</body>
62</html>
注意第26行。javascript里面函数也是对象,所以就没有必要把函数调用包装到do或者execute方法里面了,直接用()就可以让函数对象运行起来:
command();
shift和push函数是javascript中array对象的函数,可是IE5居然没有定义,最前面两个函数是为IE5准备的。
在IE和FireFox下面通过
<SCRIPT LANGUAGE="JavaScript">
<!--
if (Array.prototype.shift==null)
Array.prototype.shift = function (){
var rs = this[0];
for (var i=1;i<this.length;i++) this[i-1]=this[i]
this.length=this.length-1
return rs;
}
if (Array.prototype.push==null)
Array.prototype.push = function (){
for (var i=0;i<arguments.length;i++) this[this.length]=arguments[i];
return this.length;
}
var commandList = [];
var nAction = 0;
var functionConstructor = function(){}.constructor;
function executeCommands(){
for (var i=0;i<nAction;i++)
if (commandList.length>0){
var command = commandList.shift();
if (command.constructor == functionConstructor)
if (command.scheduleTime == null || new Date()-command.scheduleTime>0)
command();
else
commandList.push(command);
}
}
function startNewTask(){
var resultTemp = document.getElementById("sampleResult").cloneNode(true);
with (resultTemp){
id="";style.display="block";style.color=(Math.floor(Math.random()* (1<<23)).toString(16)+"00000").substring(0,6);
}
document.body.insertBefore(resultTemp,document.body.lastChild);
commandList.push(function(){simThread(resultTemp,1);});
nAction++;
}
function simThread(temp,n){
if (temp.stop) n--;
else temp.innerHTML = temp.innerHTML - (-n);
if (n<1000)
commandList.push(function(){simThread(temp,++n)});
else{
var command = function(){document.body.removeChild(temp);;nAction--;};
command.scheduleTime = new Date()-(-2000);
commandList.push(command);
}
}
setInterval("executeCommands()",1);
//-->
</SCRIPT>
<button onclick="startNewTask()">开始新线程</button>
<BR><BR>
<div id=sampleResult onmouseover="this.stop=true" onmouseout="this.stop=false" style="display:none;cursor:hand">0</div>
点击这里看效果
1<html><head><title>用command模式模拟多线程</title></head><body>
2</SCRIPT>
57<button onclick="startNewTask()">开始新线程</button>
58
59<BR><BR>
60<div id=sampleResult onmouseover="this.stop=true" onmouseout="this.stop=false" style="display:none;cursor:hand">0</div>
61</body>
62</html>
注意第26行。javascript里面函数也是对象,所以就没有必要把函数调用包装到do或者execute方法里面了,直接用()就可以让函数对象运行起来:
command();
shift和push函数是javascript中array对象的函数,可是IE5居然没有定义,最前面两个函数是为IE5准备的。
在IE和FireFox下面通过
<SCRIPT LANGUAGE="JavaScript">
<!--
if (Array.prototype.shift==null)
Array.prototype.shift = function (){
var rs = this[0];
for (var i=1;i<this.length;i++) this[i-1]=this[i]
this.length=this.length-1
return rs;
}
if (Array.prototype.push==null)
Array.prototype.push = function (){
for (var i=0;i<arguments.length;i++) this[this.length]=arguments[i];
return this.length;
}
var commandList = [];
var nAction = 0;
var functionConstructor = function(){}.constructor;
function executeCommands(){
for (var i=0;i<nAction;i++)
if (commandList.length>0){
var command = commandList.shift();
if (command.constructor == functionConstructor)
if (command.scheduleTime == null || new Date()-command.scheduleTime>0)
command();
else
commandList.push(command);
}
}
function startNewTask(){
var resultTemp = document.getElementById("sampleResult").cloneNode(true);
with (resultTemp){
id="";style.display="block";style.color=(Math.floor(Math.random()* (1<<23)).toString(16)+"00000").substring(0,6);
}
document.body.insertBefore(resultTemp,document.body.lastChild);
commandList.push(function(){simThread(resultTemp,1);});
nAction++;
}
function simThread(temp,n){
if (temp.stop) n--;
else temp.innerHTML = temp.innerHTML - (-n);
if (n<1000)
commandList.push(function(){simThread(temp,++n)});
else{
var command = function(){document.body.removeChild(temp);;nAction--;};
command.scheduleTime = new Date()-(-2000);
commandList.push(command);
}
}
setInterval("executeCommands()",1);
//-->
</SCRIPT>
<button onclick="startNewTask()">开始新线程</button>
<BR><BR>
<div id=sampleResult onmouseover="this.stop=true" onmouseout="this.stop=false" style="display:none;cursor:hand">0</div>
点击这里看效果
相关文章推荐
- 在javascript中用command模式模拟多线程
- JavaScript用Command模式模拟多线程
- 在Javascript中用command模式模拟多线程,包含注释
- 在Javascript中用command模式模拟多线程,包含注释
- Javascript模拟多线程
- Javascript模拟多线程
- 关于使用Javascript来模拟多线程的一点想法
- 用fason的参数化定时器在javascript中模拟多线程
- 用javascript模拟多线程
- 13.5.2.javascript内存与性能与13.6事件模拟
- javascript 获取多条数据(模拟ajax获取数据)
- 模拟订票系统学习多线程
- Chrome浏览器中的JavaScript多线程---WebWorker
- JavaScript 中的内存和性能、模拟事件(读书笔记思维导图)
- javascript模拟滚动条实现代码
- HTML5多线程JavaScript解决方案Web Worker——专用Worker和共享Worker
- JavaScript可否多线程? 深入理解JavaScript定时机制
- java多线程学习之死锁的模拟和避免
- SHELL模拟多线程脚本的详细注解
- JavaScript模拟鼠标右键菜单效果