You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var time = 10
var color = 'red'
function timing () {
time--
console.log(time, color)
if (time === 0) {
time = 11
if (color === 'red') {
setTimeout(function () {
color = 'green'
}, 2000)
color = 'yellow'
} else {
color = 'red'
}
}
Activity
LinStan commentedon Aug 5, 2019
基于setInterval实现
Promise
haizhilin2013 commentedon Aug 5, 2019
@LinStan 你写的红绿灯估计一般的车辆过不去啊
LinStan commentedon Aug 5, 2019
@haizhilin2013 哈哈哈,油门踩死不带怕的。
大佬 补了个Promise的。帮我看下有没有毛病,Promise和SetTimeout折腾了会
xxf1996 commentedon Aug 5, 2019
咋一看题目我还以为是设计十字路口红路灯算法那种,那可是有点复杂:smile: ;看了上面的才知道原来是循环显示红绿灯……
nowherebutup commentedon Aug 5, 2019
DarthVaderrr commentedon Aug 5, 2019
bestvow commentedon Aug 5, 2019
NicholasBaiYa commentedon Aug 6, 2019
Tttst00001 commentedon Aug 15, 2019
var time = 10
var color = 'red'
function timing () {
time--
console.log(time, color)
if (time === 0) {
time = 11
if (color === 'red') {
setTimeout(function () {
color = 'green'
}, 2000)
color = 'yellow'
} else {
color = 'red'
}
}
}
setInterval('timing()', 1000)
AruthlessMachine commentedon Aug 21, 2019
class TrafficLight {
constructor( arr ){
this.time = 0
this.index = 0
this.colors= ['red','green', 'yellow']
this.timeLens = arr
this.timer = ''
this.state = ''
this.init()
}
init(){
this.time = this.timeLens[0]
this.state = this.colors[0]
this.run()
}
run(){
console.log( this.state, this.time )
this.timer = setInterval(()=>{
if(this.time <= 1) {
clearInterval(this.timer)
this.index = (this.index+1)%3
this.time = this.timeLens[ this.index ]
this.state = this.colors[ this.index ]
this. run()
return
}
this.time -= 1
console.log( this.state, this.time )
},1000)
}
}
new TrafficLight( [6,3,1] )
AruthlessMachine commentedon Aug 21, 2019
rni-l commentedon Jan 28, 2020
hellochenk commentedon Jul 1, 2020
粘到html用浏览器打开
maoxiaoxing commentedon Dec 23, 2020
这道题应该考察的是宏任务, 下面是一个不太成熟的实现
3 remaining items