-
-
Notifications
You must be signed in to change notification settings - Fork 229
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每隔一秒打印1,2,3,4,5 #158
Comments
function delay(i) { |
for(let i=1;i<6;i++){ |
|
function print(x) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(x);
resolve();
}, 1000)
})
}
function solve() {
print(1)
.then(() => {
return print(2)
})
.then(() => {
return print(3)
})
.then(() => {
return print(4)
})
.then(() => {
return print(5)
})
}
solve(); |
function delay(i) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(i);
resolve();
}, 1000);
});
}
async function generate() {
for (let i = 1; i <= 5; i++) {
await delay(i);
}
}
generate(); |
let 块级作用域for (let i = 1; i <= 5; i++){
setTimeout(()=>console.log(i),i*1000)
} var+闭包setTimeout传参for (var i = 1; i <= 5; i++){
setTimeout((i)=>console.log(i),i*1000,i)
} 闭包传参for (var i = 1; i <= 5; i++){
((j)=>setTimeout(()=>console.log(j),j*1000))(i)
} Promiseconst delayPrint = (delay, ...args) => {
return new Promise((res, rej) => {
setTimeout(() => {
console.log(...args)
res()
},delay)
})
}
(async () => {
for (let i = 1; i <= 5; i++){
await delayPrint(1000,i)
}
})() |
|
function intervalPrint() {
let count = 0
var interval = setInterval(function () {
count++
if (count === 5) {
clearInterval(interval)
}
console.log(count)
}, 1000)
return interval
}
intervalPrint()
// promise
function intervalPrintPromise() {
return new Promise(resolve => setTimeout(resolve, 1000))
}
async function print() {
for (let number = 1; number <= 5; number++) {
await intervalPrintPromise(number).then(() => console.log(number))
}
}
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: