New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第111天 请用js编写一个红绿灯程序 #1027
Comments
基于setInterval实现
Promise
|
@LinStan 你写的红绿灯估计一般的车辆过不去啊 |
@haizhilin2013 哈哈哈,油门踩死不带怕的。 |
咋一看题目我还以为是设计十字路口红路灯算法那种,那可是有点复杂:smile: ;看了上面的才知道原来是循环显示红绿灯…… function sleep (t) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, t)
})
}
/**
* 循环显示红绿灯
* @param {number} green 绿灯显示毫秒数
* @param {number} yellow 黄灯显示毫秒数
* @param {number} red 红灯显示毫秒数
*/
async function light (green = 15000, yellow = 3000, red = 10000) {
let status = 'green'
while (true) {
await sleep(green).then(() => {
status = 'yellow'
console.log(status)
})
await sleep(yellow).then(() => {
status = 'red'
console.log(status)
})
await sleep(red).then(() => {
status = 'green'
console.log(status)
})
}
}
light(3000, 1000, 1000) |
function foo(light){
return new Promise((resolve,reject)=>{
setTimeout(() => {
console.log(light);
resolve();
}, 1000);
})
}
async function run(){
await foo('red');
await foo('green');
await foo('blue');
setTimeout(run, 0);
}
run(); |
function ryg(color, duration) {
return new Promise(function (resolve) {
setTimeout(resolve.bind(this, color), duration);
})
}
async function run() {
let light;
while (true) {
light = await ryg('red', 1000)
console.log(light)
light = await ryg('yellow', 2000)
console.log(light)
light = await ryg('green', 200)
console.log(light)
}
}
run() |
function main(index) {
const lights = ['red', 'yellow', 'green']
setTimeout(() => {
console.log(lights[index % 3])
it.next()
}, 1000)
}
function* run(times = 3) {
let init = 0
while (init < times) {
yield main(init++)
}
}
const it = run()
it.next() |
|
var time = 10 } |
class TrafficLight { } |
|
function trafficLight() {
const light = ['red', 'yellow', 'green']
let index = 0
const times = [2000, 500, 3000]
function start(_time) {
setTimeout(() => {
index += 1
if (index >= times.length) {
index = 0
}
console.log(light[index])
start(times[index])
}, _time);
}
console.log(light[index])
start(times[index])
}
trafficLight() |
<!DOCTYPE html>
<html>
<head>
<title>honglvdeng </title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.coloum div {
text-align: center;
margin: 0 auto;
}
.box div {
width: 50px;
height: 50px;
border-radius: 50%;
opacity: 0.3;
}
.red {
background: red;
}
.yellow {
background: yellow;
}
.green {
background: green;
}
</style>
</head>
<body>
<div class='root'>
<div class='box coloum'>
<div class='red'></div>
<div class='yellow'></div>
<div class='green'></div>
</div>
<div class='box horizon'>
<div class='red'></div>
<div class='yellow'></div>
<div class='green'></div>
</div>
</div>
</body>
<script type="text/javascript">
// 通行时长
const long = {
allow: 10,
warning: 3
}
// key2val
const key2val = {
coloum: 'coloum',
horizon: 'horizon'
}
const timeType = {
allow: 'allow',
warning: 'warning'
}
// 当前方向
let current = {
type: key2val.coloum,
time: timeType.allow,
long: long.allow
};
let timer = null
// child list
const coloum = document.getElementsByClassName('coloum')[0].children;
const horizon = document.getElementsByClassName('horizon')[0].children;
const clearNode = () => {
coloum[0].style.opacity = 0.3;
coloum[1].style.opacity = 0.3;
coloum[2].style.opacity = 0.3;
horizon[0].style.opacity = 0.3;
horizon[1].style.opacity = 0.3;
horizon[2].style.opacity = 0.3;
}
// 南北通行
const showns = () => {
coloum[2].style.opacity = 1;
horizon[0].style.opacity = 1;
}
// 东西通行
const showew = () => {
coloum[0].style.opacity = 1;
horizon[2].style.opacity = 1;
}
const warningns = () => {
coloum[1].style.opacity = 1;
horizon[0].style.opacity = 1;
}
const warningew = () => {
coloum[0].style.opacity = 1;
horizon[1].style.opacity = 1;
}
const run = (currt) => {
clearNode();
timer = null;
switch(currt.type) {
// 东西
case key2val.coloum:
if(currt.time === timeType.allow) {
console.log('南北绿灯')
// 允许 通行
showns();
const t = currt.long;
currt = {
type: key2val.coloum,
time: timeType.warning,
long: long.warning
}
timer = setTimeout(() => run(currt), t *1000)
// setTimeout(() => console.log(currt), currt.long *1000)
} else {
console.log('南北黄灯')
// 警告
warningns();
const t = currt.long;
currt = {
type: key2val.horizon,
time: timeType.allow,
long: long.allow
}
timer = setTimeout(() => run(currt), t *1000)
}
break;
// 南北
case key2val.horizon:
if(currt.time === timeType.allow) {
console.log('东西绿灯')
// 允许 通行
showew();
const t = currt.long;
currt = {
type: key2val.horizon,
time: timeType.warning,
long: long.warning
}
timer = setTimeout(() => run(currt), t *1000)
} else {
console.log('东西黄灯')
// 警告
warningew();
const t = currt.long;
currt = {
type: key2val.coloum,
time: timeType.allow,
long: long.allow
}
timer = setTimeout(() => run(currt), t *1000)
}
break;
}
}
run(current);
</script>
</html> 粘到html用浏览器打开 |
这道题应该考察的是宏任务, 下面是一个不太成熟的实现
|
随便搞了一个 function changeLight(light, time) {
return new Promise(resolve => {
console.log('现在为:', light);
setTimeout(() => {
resolve();
}, time);
})
}
async function trafficLight() {
await changeLight('red', 3000);
await changeLight('yellow', 1000);
await changeLight('green', 3000);
await changeLight('yellow',1000).then(()=>{
trafficLight()
})
}
trafficLight(); |
generator实现 function print (light) {
console.log('%c%s', `color: ${light.color};background:black;`, light.color)
// return sleep(light.duration)
return new Promise(resolve => {
setTimeout(resolve, light.duration)
})
}
function* trafficLight () {
var lights = [
{ color: 'red', duration: 3000 },
{ color: 'yellow', duration: 2000 },
{ color: 'green', duration: 1000 },
]
for (var i = 0; i < lights.length; i++) {
yield print(lights[i])
}
}
function main () {
var gen = trafficLight()
var result = {}
!function run () {
result = gen.next()
console.log(result)
if(result.done) {
main()
} else {
result.value.then(run)
}
}()
}
main() |
function sleep(color, times) { async function run() { |
第111天 请用js编写一个红绿灯程序
The text was updated successfully, but these errors were encountered: