1.话不多说,直接上代码
<!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>
</head>
<body>
<div id="light">
<span></span>
</div>
</body>
<script>
const lights = [
{
color: 'red', delay: '5000',
color: 'yellow', delay: '2000',
color: 'green', delay: '5000',
color: 'balck', delay: '5000',
}
]
const box = document.getElementById('light')
const changeLight = () => {
box.style.background = 'red'
console.log('red11');
setTimeout(() => {
box.style.background = 'yellow'
console.log('yellow');
setTimeout(() => {
box.style.background = 'green'
console.log('green');
setTimeout(() => {
changeLight()
}, 5000)
}, 2000)
}, 5000)
}
changeLight()
</script>
</html>