JS模拟多线程
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>
相关文章
JS实现屏蔽网页右键复制及ctrl+c复制的方法【2种方法】
这篇文章主要介绍了JS实现屏蔽网页右键复制及ctrl+c复制的方法,结合实例形式分析了2种比较常用的屏蔽复制功能的技巧,需要的朋友可以参考下2016-09-09javascript设计模式 – 观察者模式原理与用法实例分析
这篇文章主要介绍了javascript设计模式 – 观察者模式,结合实例形式分析了javascript观察者模式相关概念、原理、用法及操作注意事项,需要的朋友可以参考下2020-04-04JavaScript JSON.stringify()的使用总结
JSON是一种轻量级数据格式,可以方便地表示复杂数据结构。JSON对象有两个方法:stringify()和parse()。在简单的情况下,这两个方法分别可以将JavaScript序列化为JSON字符串,以及将JSON解析为原生JavaScript值。本文着重介绍JSON.stringify()的使用方法和注意事项。2021-05-05UniApp中Scroll-View设置占满下方剩余高度的方法记录
在使用uniapp开发项目过程中有时候会想让一些组件占有屏幕剩余的高度,下面这篇文章主要给大家介绍了关于UniApp中Scroll-View设置占满下方剩余高度的方法,需要的朋友可以参考下2023-04-04
最新评论