Skip to content

Instantly share code, notes, and snippets.

@303182519
Created May 23, 2016 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 303182519/880a305659b01905f43e5e3cac9e823e to your computer and use it in GitHub Desktop.
Save 303182519/880a305659b01905f43e5e3cac9e823e to your computer and use it in GitHub Desktop.
一个多进程的通道
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.way {
overflow:hidden;
margin-bottom:10px;
}
.way em{
float:left;
}
.way span {
float:right;
background:#000;
width:0;
height:20px;
color:#fff;
}
</style>
</head>
<body>
<div id="div0" class="way"><em>通道0:</em><span></span></div>
<div id="div1" class="way"><em>通道1:</em><span></span></div>
<div id="div2" class="way"><em>通道2:</em><span></span></div>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var num = -1;
var wayName = new String('xxxx');
var moveTime = [8000,5000,3000];
//每个线程内部的时间
var procressTime = [0,0,0];
//添加礼物
function addGift(giftNum){
var minTime = _.min(procressTime);
var selWay = procressTime.indexOf(minTime);
var mx = 'div'+selWay;
//主要是第一次的话,要动起来
var arr = $.queue(wayName, mx);
$.queue(wayName, mx, function(next){
//console.log(giftNum);
$('#'+mx).find('span').text(giftNum).animate({width: 300}, moveTime[selWay],function(){
$(this).css('width',0);
procressTime[selWay] -= moveTime[selWay];
/*console.log('当前的线程为:'+selWay);
console.log(procressTime[selWay]);
console.log('-------------------------');*/
next();
})
});
procressTime[selWay] += moveTime[selWay];
//主要是第一次的话,要动起来
if(arr && arr.length == 0){
$.dequeue(wayName, mx);
}
}
var giftNum = 1;
//这里模拟后台定时发送礼物请求
var timer = setInterval(function(){
addGift(giftNum++);
}, 1000);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment