当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JS模拟多线程

Javascript
二级连动的另一个不错的实现方法
键盘元素的控制小方块的移动效果
如何丰富alt属性的显示效果
鼠标移动到某个单元格上后,整个列都变色的实现方法
用JavaScript实现字符串切分功能
通过JAVAScript实现页面自适应
短信提示使用 特效
一个多次搜索+多次传值的解决方案
javascript读取RSS数据
WEB泡泡堂2.0(图形界面+电脑对玩)(javascript)
JS+FLASH幻灯片播放图片脚本,整理了代码,使得调用更加方便!
利用javascript中的call实现继承
javascript 中对象的继承〔转贴〕
float ad浮动广告代码
showModalDialog 和 showModelessDialog
Ajax::prototype 源码解读
深入认识JavaScript中的函数
Javascript技术技巧大全(五)
通过MSXML2自动获取QQ个人头像及在线情况(给初学者)
用正则xmlHttp实现的偷(转)

Javascript 中的 JS模拟多线程


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 87 ::
收藏到网摘: n/a

var Thread = {
runNum : 0, //当前正式运行的线程数
maxNum : 10, //最大同时执行的线程数 -1表示不限
commandList : new Array(),
start : function(){
//window.status = this.runNum;
if(this.maxNum != -1 && this.runNum >= this.maxNum){
return;
}
if(this.commandList.length <= 0){
this.runNum = 0;
return false;
}
this.runNum++;
var _this =this;
var tFun = function(){
if(!_this.commandList[0]) return;
var command = _this.commandList[0].shift();
command.apply(_this,_this.commandList[0].concat(
function(){ //alert(2)
if(_this.runNum > 0)_this.runNum--;
setTimeout(function(){_this.start.apply(_this)},1);
}));
_this.commandList.shift();
}
setTimeout(tFun,1);
setTimeout(function(){_this.start.apply(_this)},10);
}
}

<script src="prototype.js"></script>
<script type="text/javascript" defer="defer">
function test(obj,info,callback){
callback = callback || new Function();
new Ajax.Request('test.xml',{method:'get',
onSuccess:function(o){
$(obj).innerHTML += info + '完成<br>';
callback('完成');
},
onFailure : function(o){
$(obj).innerHTML += info + '失败<br>';
callback('失败');
},
onComplete : function(o){
document.body.scrollTop = 9999;
}
});
}

var Thread = {
runNum : 0, //当前正式运行的线程数
maxNum : 5, //最大同时执行的线程数 -1表示不限
commandList : new Array(),
start : function(){
//window.status = this.runNum;
if(this.maxNum != -1 && this.runNum >= this.maxNum){
return;
}
if(this.commandList.length <= 0){
this.runNum = 0;
return false;
}
this.runNum++;
var _this =this;
var tFun = function(){
if(!_this.commandList[0]) return;
var command = _this.commandList[0].shift();
command.apply(_this,_this.commandList[0].concat(
function(){ //alert(2)
if(_this.runNum > 0)_this.runNum--;
setTimeout(function(){_this.start.apply(_this)},1);
}));
_this.commandList.shift();
}
setTimeout(tFun,1);
setTimeout(function(){_this.start.apply(_this)},10);
}
}

for(var i = 0; i < 100 ; i++){
Thread.commandList.push(new Array(test,document.body,i+1));
};
Thread.start();
</script>
<body>
</body>