-
Notifications
You must be signed in to change notification settings - Fork 6
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
第 2 期(2019-05-09):定时输出数字 #3
Comments
const outputNumber = () => {
const max = 5;
let i = max;
while (i) {
setTimeout(i => {
console.log(max + 1 - i);
},1000 * i, i--);
}
}; |
// 利用了 let
const printNumber = () => {
for (let i = 5; i > 0; i--) {
setTimeout(() => {
console.log(i)
}, (6 - i) * 1000)
}
}
// 利用了闭包
const printNumber = () => {
for (var i = 5; i > 0; i--) {
(function (i) {
setTimeout(() => {
console.log(i)
}, (6 - i) * 1000)
})(i)
}
}
// 利用了闭包
const printNumber = () => {
for (var i = 5; i > 0; i--) {
setTimeout(((i) => {
return () => {
console.log(i);
}
})(i), (6 - i) * 1000);
}
} |
const printNumber = number => {
console.log(number--);
number && setTimeout(() => printNumber(number), 1000);
};
printNumber(5); |
//setTimeout循环和条件停止循环
//参考网址:https://blog.csdn.net/qq_28256783/article/details/80097092
var a=6;
function printN(){
if(a>1){
a=a-1;
console.log(a);
setTimeout("printN()",1000)
}
}
printN(); |
好优雅的代码 👍 ,不过第一个数字是直接打印出来的,根据题意,所有的数字都应该隔1s才打印 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
编写一个方法,每隔 1s 依次在控制台打印出
5, 4, 3, 2, 1
参考答案:
请参考 @liwenkang 的答案,很全面。
@Wxh16144 的答案也不错,代码很优雅。
本期优秀回答者: @liwenkang
The text was updated successfully, but these errors were encountered: