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

Javascript
一定时间滚动的链接菜单效果
超链接的陷下效果
用js实现的打字效果的带链接的新闻标题
js实现的很酷的连接提示效果
再谈IE中Flash控件的自动激活 ObjectWrap
超级兔子让浮动层消失的前因后果
在线编辑器的实现原理(兼容IE和FireFox)
B/S开发中常用javaScript技术与代码
用JavaScript事件串连执行多个处理过程的方法
Javascript中的Split使用方法与技巧
HTML在线编辑器的基本概念与相关资料
Js+DVML很酷漂亮实用的右键弹出菜单
用js实现的比较经典实用的触发型导航菜单
js中slice()方法的使用说明
基础的prototype.js常用函数及其用法
常用限制input的方法的js代码
又一个图片自动缩小的JS代码
JavaScript触发器详解
理解Javascript的caller,callee,call,apply区别
一个简单的日历代码 (For: FF1+ IE5+ Opr7+)测试

Javascript 中的 JS模拟多线程


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 130 ::
收藏到网摘: 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>