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

Javascript
在js中使用"with"语句中跨frame的变量引用问题
原型方法的不同写法居然会影响调试的解决方法
在JavaScript中遭遇级联表达式陷阱
JScript内置对象Array中元素的删除方法
获取JavaScript用户自定义类的类名称的代码
使用TextRange获取输入框中光标的位置的代码
使用onbeforeunload属性后的副作用
IE7提供XMLHttpRequest对象为兼容
encode脚本和normal脚本混用的问题与解决方法
用js判断用户浏览器是否是XP SP2的IE6
关于使用runtimeStyle属性问题讨论文章
javascript学习随笔(使用window和frame)的技巧
javascript学习随笔(编写浏览器脚本 Navigator Scripting )
跑马灯效果大全
让超链接显示提示信息的js代码
打开超链需要“确认”对话框的方法
javascript的对话框详解与参数
让网页上的超链接失效,不能点击的js代码
鼠标经过时链接文字的特别震撼的显示效果
显示页面的所有链接的js代码

Javascript 中的 JS模拟多线程


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