印客学院
用户9618
分享
14. 编程题(50题)
输入“/”快速插入内容
14.
编程题(50
题
)
用户9618
用户9618
用户6449
用户6449
用户9254
用户9254
用户6423
用户6423
用户3527
用户3527
用户2886
用户2886
2月27日修改
1.
使用Promise实现红绿灯交替重复亮
红灯3秒亮一次,绿灯2秒亮一次,黄灯1秒亮一次;如何让三个灯不断交替重复亮灯?
要求:用Promise实现
三个亮灯函数已经存在:
代码块
JavaScript
function red() {
console.log('red');
}
function green() {
console.log('green');
}
function yellow() {
console.log('yellow');
}
参考答案:
代码块
JavaScript
function red() {
console.log("red");
}
function green() {
console.log("green");
}
function yellow() {
console.log("yellow");
}
const light = function (timer, cb) {
return new Promise(resolve => {
setTimeout(() => {
cb()
resolve()
}, timer)
})
}
const step = function () {
Promise.resolve().then(() => {
return light(3000, red)
}).then(() => {
return light(2000, green)
}).then(() => {
return light(1000, yellow)
}).then(() => {
return step()
})
}
step();
2.
bind、call、apply 有什么区别?如何实现一个bind?