红绿灯
<!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>
.item{
border: 1px solid black;
width: 50px;
height: 50px;
margin: 10px;
}
.red{
background-color: red;
}
.green{
background-color: green;
}
.yellow{
background-color: yellow;
}
</style>
</head>
<body>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<script>
let gloal=['red','yellow','green'];
let gloalindex=0;
color='red';
Object.defineProperty(this,'color',{
configurable:true,
enumerable:true,
get(){
return this._color||'none';
},
set(newvalue){
let beforenode=document.getElementsByClassName(color)
if(beforenode.length>0)
beforenode[0].setAttribute('class','item');
this._color=newvalue;
document.getElementsByTagName('div')[gloalindex].classList.add(newvalue);
return;
}
})
setInterval(()=>{
gloalindex=(gloalindex+1)%3;
color=gloal[gloalindex];
console.log(_color)
},1000)
</script>
</body>
</html>