Skip to content
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

Open
Sunny-117 opened this issue Nov 3, 2022 · 8 comments
Open

js每隔一秒打印1,2,3,4,5 #158

Sunny-117 opened this issue Nov 3, 2022 · 8 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@hyxieshi
Copy link

function delay(i) {
return new Promise((res) => {
setTimeout(() => {
console.log(i);
res();
}, 1000);
});
}
async function tim() {
for (let i = 1; i < 6; i++) {
await delay(i);
}
}
tim()

@yuxinye65
Copy link

for(let i=1;i<6;i++){
setTimeout(() => {
console.log(i)
},i*1000)
}

@LifeIsTerrible
Copy link

function delayLog(str) {
    return new Promise((resolve, _) => {
        setTimeout(() => {
            console.log(str);
            resolve()
        }, 1000);
    })
}
async function time() {
    for (let i = 1; i <= 5; i++) {
        await delayLog(i)
    }
}
time();

@Pcjmy
Copy link
Contributor

Pcjmy commented Mar 9, 2023

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();

@bearki99
Copy link

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();

@2239351258
Copy link

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)
}

Promise

const 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)
  }
})()

@CHJ30
Copy link

CHJ30 commented Nov 2, 2023

let a=1 let x = setInterval(()=>{ console.log(a++) if(a===6){ clearInterval(x) x = null } },1000)

@xianjianlf2
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants