信号量等同于累加器
放到函数外面
xinhaoliang=0;
function(){
点击一次
xinhaoliang++;
累加一次
}
案例:
```
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
li{
list-style: none;
}
.lunbotu{
width:658px;
height:206px;
border:2px solid red;
margin:100px auto;
position:relative;
}
.lunbotu .anniu ul li{
width:50px;
height:25px;
background-color: rgb(241, 241, 28);
position:absolute;
top:50%;
margin-top:-12.5px;
text-align: center;
line-height:25px;
display:block;
cursor:pointer;
}
.lunbotu .anniu ul li.you{
right:0;
}
</style>
</head>
<body>
<div class="lunbotu">
<a href="#"><img src="./img/1.jpg" alt="" id="tu"></a>
<div class="anniu">
<ul>
<li id="box">左</li>
<li class="you" id="box1">右</li>
</ul>
</div>
</div>
</body>
</html>
<script type="text/javascript">
//获取点按钮
var you=document.getElementById("box1");
//获取图片
var tu=document.getElementById("tu");
//按钮绑定单机事件
var xinhao=1;
//信号量
you.onclick=function(){
xinhao++;
if(xinhao>2){xinhao=1};
tu.src="./img/"+xinhao+".jpg";
}
var zuo=document.getElementById("box");
zuo.onclick=function(){
if(xinhao>1){
xinhao--;
tu.src="./img/"+xinhao+".jpg";
}
}
</script>
```