js-challenges icon indicating copy to clipboard operation
js-challenges copied to clipboard

使用 Promise 实现每隔三秒输出时间

Open Sunny-117 opened this issue 3 years ago • 4 comments

Sunny-117 avatar Nov 03 '22 08:11 Sunny-117

const task = (timer) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log(new Date())
      resolve()
    }, timer);
  })
}

const taskRunner = async () => {
  await task(3000);
  taskRunner();
}
taskRunner();

mengqiuleo avatar Jan 17 '23 20:01 mengqiuleo

function showTime(){
    return new Promise((resolve, reject)=>{
        setTimeout(()=>{
            console.log(new Date());
            resolve();
        }, 3000);
    })
}
const time = async () => {
    await showTime();
    time();
}
time();

bearki99 avatar Feb 13 '23 02:02 bearki99

const task = (timer) => {
    return new Promise((resovle) =>
        setTimeout(() => {
            console.log(1);
            resovle();
            task(timer);
        }, timer)
    );
};

cscty avatar Jun 19 '23 15:06 cscty

function time() { let promise = new Promise(resolve => { setTimeout(() => { console.log(new Date()); resolve() }, 3000) }) promise.then(() => time()) }

GISpjd avatar Jul 19 '24 07:07 GISpjd