JS模擬多線程
runNum : 0, //當前正式運行的線程數(shù)
maxNum : 10, //最大同時執(zhí)行的線程數(shù) -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, //當前正式運行的線程數(shù)
maxNum : 5, //最大同時執(zhí)行的線程數(shù) -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模擬的Map類實現(xiàn)方法
- js模擬點擊事件實現(xiàn)代碼
- js模擬滾動條(橫向豎向)
- js模擬點擊以提交表單為例兼容主流瀏覽器
- javascript用DIV模擬彈出窗口_窗體滾動跟隨
- javascript模擬滾動條實現(xiàn)代碼
- JS組件Bootstrap實現(xiàn)下拉菜單效果代碼
- 基于AngularJs + Bootstrap + AngularStrap相結(jié)合實現(xiàn)省市區(qū)聯(lián)動代碼
- JS組件Bootstrap Table使用實例分享
- 三級下拉菜單的js實現(xiàn)代碼
- JS實多級聯(lián)動下拉菜單類,簡單實現(xiàn)省市區(qū)聯(lián)動菜單!
- Js點擊彈出下拉菜單效果實例
- JS模擬bootstrap下拉菜單效果實例
相關(guān)文章
JavaScript 5 新增 Array 方法實現(xiàn)介紹
JavaScript 5 發(fā)布有一段時間了,Array 對象新增了很多方法。但在老版本的瀏覽器上還不能使用,得益于 JavaScript 的動態(tài)可擴展性,我實現(xiàn)了這些方法,在此曬出來與君共勉2012-02-02

