interval只是间隔,下面的例子很容易看出来他始终是一条线程function aa() { var ss = document.createElement("div"); document.body.appendChild(ss); for(var i=0;i<2000;i++)ss.innerHTML = i; } var ee = setInterval(aa,10); document.onmousedown = function(){clearInterval(ee);}但如果加点手脚 var ee = setInterval(setTimeout(aa,2000),10); 这样当你按下鼠标停止interval的时候仍然会继续执行几次 这表示已经开始了多条线程 至于后面如何应用就看你自己的了
不好意思改成 var ee = setInterval(function(){setTimeout(aa,2000)},10);
但如果加点手脚 var ee = setInterval(setTimeout(aa,2000),10); 这样当你按下鼠标停止interval的时候仍然会继续执行几次 这表示已经开始了多条线程 至于后面如何应用就看你自己的了回gzdiablo() : 那主要是因为有个timeout,而不是多线程吧?据我粗浅的认识,异步的回调才可能是多线程?
回gzdiablo() :插了几个桩试试:var count = new Object; count.n = 0; var actions = new Array; function aa(count) { actions.push('start ' + count.n); var ss = document.createElement("div"); document.body.appendChild(ss); for(var i=0;i<2000;i++) ss.innerHTML = i; actions.push('finish ' + count.n); count.n ++; } var ee = setInterval(function(){setTimeout(aa,200, count);},10); document.onmousedown = function(){clearInterval(ee);}插得不是很好,但不妨碍看结果,出来的结果都是start/finish成对出现的,后面的数字也匹配。可见还是单线的
JS在IE浏览器中只能模拟多线程,可以和客户协商,把那些步骤分成一些零碎的步骤人为触发来进行下面有一个EMU的模拟代码 <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.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>
{
var ss = document.createElement("div");
document.body.appendChild(ss);
for(var i=0;i<2000;i++)ss.innerHTML = i;
}
var ee = setInterval(aa,10);
document.onmousedown = function(){clearInterval(ee);}但如果加点手脚
var ee = setInterval(setTimeout(aa,2000),10);
这样当你按下鼠标停止interval的时候仍然会继续执行几次 这表示已经开始了多条线程
至于后面如何应用就看你自己的了
var ee = setInterval(function(){setTimeout(aa,2000)},10);
var ee = setInterval(setTimeout(aa,2000),10);
这样当你按下鼠标停止interval的时候仍然会继续执行几次 这表示已经开始了多条线程
至于后面如何应用就看你自己的了回gzdiablo() :
那主要是因为有个timeout,而不是多线程吧?据我粗浅的认识,异步的回调才可能是多线程?
count.n = 0;
var actions = new Array;
function aa(count)
{
actions.push('start ' + count.n);
var ss = document.createElement("div");
document.body.appendChild(ss);
for(var i=0;i<2000;i++)
ss.innerHTML = i;
actions.push('finish ' + count.n);
count.n ++;
}
var ee = setInterval(function(){setTimeout(aa,200, count);},10);
document.onmousedown = function(){clearInterval(ee);}插得不是很好,但不妨碍看结果,出来的结果都是start/finish成对出现的,后面的数字也匹配。可见还是单线的
<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.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>
if (Array.prototype.shift==null)
我之前写了一个树,因为使用settimeout导致 节点位置都乱了和多线程很类似 所以一直以为是开启了多线程 导致理解错误
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++;
alert('dd');
}按说,线程应该阻塞了?
但这个数字仍然在变。我试着把interval设为1000,alert仍然没有阻塞,说明是两个线程?谁来解释一下?
2. 先前贴子里那个alert阻塞了哪个线程?
iframe果然不错。不过IE的alert很BT呢。。<iframe style="display:none;"></iframe>
<script type="text/javascript">
window.onload = function () {
var frame = window.frames[0], i = 0;
var func = frame.window.func = function () {
document.getElementById("status").innerHTML = i ++;
frame.window.setTimeout(func, 1000);
}
frame.window.func();
window.setTimeout("alert(1)", 5000);
};
</script>
<div id="status"></div>
IE的this指的貌似有点问题
frame.window.setTimeout(this.func, 10);在IE测试this哪也没指貌似。。在FF测试this指的就是frame.window;
用Iframe处理可以并行的发一些请求,但本身自己的复杂性至少要上升一个数量级?
包括sibling间的交互.top-iframes间的交互。现在一个场景:
iframe中的回调在更改top.window或兄弟iframe中的一个数组,而这个数组正在迭代,这时候是怎样的顺序处理呢?
2. 先前贴子里那个alert阻塞了哪个线程?各位请费神看看。我觉得对大家搞清楚这些问题都有些帮助的,不是么?
对于
2. 先前贴子里那个alert阻塞了哪个线程? 问题。。应该已经是答案了吧。。对于XMLHTTP貌似可以实现多线程的效果不过没具体测试过
因为它可以实现一次弹出多个alert(在IE里)
http://www.zhb.org.cn/wc/ajax/
这个是xmlHTTP的测试。。IE是若干个alert,FF则是一个下面的code是测试iframe的IE和FF差别结果。。<iframe style="display:none;"></iframe>
<script type="text/javascript">
window.onload = function () {
var frame = window.frames[0], i = 0;
var func = frame.window.func = function () {
document.getElementById("status").innerHTML = i ++;
frame.window.setTimeout(func, 1000); //这个IFRAME的ALERT不会阻止
//另外iframe里还可以这样写IE则不行
//*****this.setTimeout(this.func, 1000)
//window.setTimeout(func, 1000); //这个则会阻止
}
frame.window.func();
window.setTimeout("alert(1)", 5000);
};
</script>
<div id="status"></div>
FF在这种情况下,alert并不是不阻塞。
而是阻塞很短一段时间。也就是说alert本身是和函数在“抢”时间干活
1. 异步回调是不是多线程?
2. 先前贴子里那个alert阻塞了哪个线程?首先,一个page在脚本运行只有一个thread,如果能够调用activeX做的muti-thread,那是另外一回事。应该说这两个问题都是不属于脚本方面的事了
因为如果你用XMLHTTP的话,XMLHTTP的异步是ActiveX(ie7.0以下版本)管的事,activeX拥有多线程的性质。ActiveX本身的性质发出的request,属脚本不可控制范围(除了几个API)。但如果回到脚本之后,仍然是单线程。题外的一些话:alert在IE里和mozilla里都不一样的,但它们的参考里都是这个意思:
Displays a dialog box containing an application-defined message。
但mozilla和IE的kernel不同,这得看具体情况而定about:
Iframe很好
用Iframe处理可以并行的发一些请求,但本身自己的复杂性至少要上升一个数量级?
包括sibling间的交互.top-iframes间的交互。这个要看怎么处理了,这中间多了一个交互的代码,关键看怎么处理和控制iframe。封装得好的话应该问题不大,把iframe看作是一个thread对象,用一个object来管理,用一个array当作线程池。
---------------------------------------------------还是这个场景:
iframe中的回调在更改top.window或兄弟iframe中的一个数组,而这个数组正在迭代,这时候是怎样的顺序处理呢?还是两个线程同时干活?如果这样的话是不是处理的时候还要给这个数组加锁以保持数据的一致性?
因为如果你用XMLHTTP的话,XMLHTTP的异步是ActiveX(ie7.0以下版本)管的事,activeX拥有多线程的性质。ActiveX本身的性质发出的request,属脚本不可控制范围(除了几个API)。但如果回到脚本之后,仍然是单线程。
-----------------------------------------------------
事例:http://www.zhb.org.cn/wc/ajax/那IE在这里的两个alert怎么解释呢?PS:看样子 FF 想用XMLHTTP的异步请求来玩多线程是不可能了?但这个例子中又牵涉到了alert的机制,还下不了定论