改进了一下,现在允许在一个运行周期里面运行多个command,同时提供了对线程延迟的支持(见executeCommands函数中)。任务是计算1+2+3...+1000累加的结果。现在让每个任务运行完成后延迟2秒再清楚自己的运行结果。点击一次鼠标就建立一个线程。<html>
<head>
<title>emu -- 用command模式模拟多线程</title>
</head>
<body>
<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.random()*(2<<23.5);
}
document.body.insertBefore(resultTemp,document.body.lastChild);
commandList.push(function(){simThread(resultTemp,1);});
nAction++;
}function simThread(temp,n){
if (temp.stop) n--;
else temp.innerText = temp.innerText-(-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);
}
}window.onload = function(){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>
</body>
</html>看了一下cpu占用,开1个任务和10个任务的cpu占用基本上没有区别。其实真正用在任务上的cpu时间是很少的。
<head>
<title>emu -- 用command模式模拟多线程</title>
</head>
<body>
<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.random()*(2<<23.5);
}
document.body.insertBefore(resultTemp,document.body.lastChild);
commandList.push(function(){simThread(resultTemp,1);});
nAction++;
}function simThread(temp,n){
if (temp.stop) n--;
else temp.innerText = temp.innerText-(-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);
}
}window.onload = function(){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>
</body>
</html>看了一下cpu占用,开1个任务和10个任务的cpu占用基本上没有区别。其实真正用在任务上的cpu时间是很少的。
<head>
<title>emu -- 用command模式模拟多线程</title>
</head>
<body>
<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.random()*(2<<23.5);
}
document.body.insertBefore(resultTemp,document.body.lastChild);
commandList.push(function(){simThread(resultTemp,1);});
nAction++;
}function simThread(temp,n){
if (temp.stop) n--;
else temp.innerText = temp.innerText-(-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);
}
}window.onload = function(){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>
</body>
</html>看了一下cpu占用,开1个任务和10个任务的cpu占用基本上没有区别。其实真正用在任务上的cpu时间是很少的。
<head>
<title>emu -- 用command模式模拟多线程</title>
</head>
<body>
<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);
}
}window.onload = function(){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>
</body>
</html>主要是改了: else temp.innerHTML = temp.innerHTML - (-n);
和:style.color=(Math.floor(Math.random()* (1<<23)).toString(16)+"00000").substring(0,6);
/****************************************************************
* 函数名:fnDo
* 功能描述:向指定的DIV/SPAN中写入异步带回的HTML代码
* 输入:
* 版本:2005-05-27 by lt
/***************************************************************/
function fnDo(ID,sDiv,sUrl)
{
sDiv.innerHTML = "Loading..."
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
var xmlDom = new ActiveXObject("Msxml2.DOMDocument");
var strURL = sUrl + ID;
xmlhttp.Open("POST",strURL , true);
xmlhttp.onreadystatechange = function(){
var state = xmlhttp.readyState;
var xmlDom = new ActiveXObject("Msxml2.DOMDocument");
if (state == 4)
{
xmlDom.loadXML(xmlhttp.responseXML.xml);
//alert(xmlDom.documentElement.selectSingleNode("//objXML").text)
var getInfo = xmlDom.documentElement.selectSingleNode("//objXML").text;
sDiv.innerHTML = getInfo
}
};
xmlhttp.Send(xmlDom);
return 1;
}
//--------------------------------------------------------
</script>
<div id="divCategroy"></div>
<div id="divContent"></div>
<script language="javascript">
fnDo(0,divCategroy,"getcategory.asp?ID=")
fnDo(0,divContent,"getcategory.asp?ID=")
</script>
"其实command模式只是用来模拟多线程而已,并不是真的实现了多线程"----
不少朋友喏喏称是,所以正在怀疑自己呢,
function simThread(temp,n){
在这一句后加了一句:
if(Math.random()>0.99) alert();
测试结果分明就是单线程啊 捊须深思中。
但是多个窗口里的script可以同时运行,也可以当多线程看,
还有window.showModelessDialog()它事实是也是多提供一个线程。回复人: a040liutao(冬之心) ( ) 信誉:100 2005-06-10 10:25:00 得分: 0
的问题,可以参考:
http://jkisjk.vip.sina.com/html/getDataFromServer.htm 的第一种方式
<head>
<title></title>
</head>
<body>
<button onclick="newTask()">new task</button>
<SCRIPT LANGUAGE="JavaScript">
<!--
var framesCount = 0;
function newTask(){
var elm = document.createElement("iframe");
document.body.insertBefore(elm);
elm.style.display="none";
framesCount++;
document.frames[framesCount-1].document.write("<script>for (var i=0,n=0;i<=1000000;n+=i++);alert(n)<\/script>")
}
//-->
</SCRIPT>
</body>
</html>点击按钮的时候开始执行运算,在运算结束前没有办法再点击一次按钮来开启一个新线程的。
modaldialog是肯定会堵塞主窗口的。开新窗口还有点用:
<html>
<head>
<title></title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
function newTask(left){
var win = window.open("","","top=300,height=100,status=yes,width=300,left="+left);
win.document.write("<script>setTimeout('for (var i=0,n=0;i<=10001;n+=i++)status=n;',1000)<\/script>")
}
newTask(0);
newTask(330);
newTask(660);
//-->
</SCRIPT>
</body>
</html>
<head>
<title></title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!--
var count1=0;
var count2=0;
var elm = document.createElement("iframe");
document.body.insertBefore(elm);
elm.style.display="none";
document.frames[0].document.write("<script>setTimeout(\"for (var i=0,n=0;i<=10001;n+=i++){parent.count1=n;parent.status=parent.count1+' - '+parent.count2};\",1000)<\/script>")var elm = document.createElement("iframe");
document.body.insertBefore(elm);
elm.style.display="none";
document.frames[1].document.write("<script>setTimeout(\"for (var i=0,n=0;i<=10001;n+=i++){parent.count2=n;parent.status=parent.count1+' - '+parent.count2};\",1000)<\/script>")
//-->
</SCRIPT>
</body>
</html>
<iframe src="a1.html" > </iframe>
<iframe src="a1.html" > </iframe>a1.html------------
<script language=javascript >
alert();
</script>两个alert不能同时出现,证明不是真正的多线程
开多个窗口,让多个窗口里的script各自执行,来模拟多线程应该可行。下例是非模态alert,也应该算是模拟多线程的一种应用:
http://jkisjk.vip.sina.com/html/closeAlertBox.htm
可能是有时网络不好
showModelessDialog是我所知道的唯一能够堵塞正在运行的进程的方法,怎么可能用它来模拟多线程呢?你没有办法同时显示多个模式对话框,因为显示第一个的时候主页面的线程就停下来等了,一直到关掉它第二个才有可能打得开。
也应该算是模拟多线程的一种应用,可以同时弹出多个alert框
用的是showModelessDialog方式
因为当时看到有朋友发贴要求“自动关闭alert对话框”,
所以写了个来凑热闹,尽管没有实际用途。----捊须而笑
附庸下风雅,辱没次期文,
聊以自娱,料无大妨刮胡子刀就免了,
这些事让我儿我女婿送就得,
不敢劳stone大驾。捊须而笑--
onreadystatechange我也试过,其中一个onreadystatechange执行阻塞,其余onreadystatechange永远都没机会执行.
可以这么测试一下:
用两个iframe加载两个htm,其中一个iframe的onreadystatechange中使用模式对话框
你会发现,先加载完成的页面显示模式对话框,在未被确认之前,第二个页面永远没有机会加载完成.
x.htm /*****直接在地址栏输入x.htm并回车,这时候你可能看不到两个alert同时出现的情况,并且在这种情况下,必须将该alert消息确认之后,两个文档才加载完成
<script>
function test()
{
var sDialogHW = "dialogWidth:350px; dialogHeight:250px;status:no;";
var sDialogULR = "about:blank";
retDlg=window.showModalDialog(sDialogULR,null,sDialogHW);
}
</script>
<iframe onreadystatechange="test()" src="y.htm"></iframe>
<iframe onreadystatechange="test()" src="z.htm"></iframe>
y.htm
<script>
alert(1)
</script>
<b>finish</b>
z.htm
<script>
alert(2)
</script>
<b>finish2</b>