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

【Q754】实现 LazyMan #810

Open
shfshanyue opened this issue Apr 4, 2024 · 2 comments
Open

【Q754】实现 LazyMan #810

shfshanyue opened this issue Apr 4, 2024 · 2 comments
Labels

Comments

@shfshanyue
Copy link
Owner

shfshanyue commented Apr 4, 2024

LazyMan('Hank')
// 输出:
// Hi!This is Hank!


LazyMan('Hank').sleep(10).eat('dinner')
// 输出:
// Hi! This is Hank!
// 等待10秒..
// Wake up after 10
// Eat dinner~


LazyMan('Hank').eat('dinner').eat('supper')
// 输出:
// Hi This is Hank!
// Eat dinner~
// Eat supper~


LazyMan('Hank').sleepFirst(5).eat('supper')
// 输出:
// 等待5秒..
// Wake up after 5
// Hi This is Hank!
// Eat supper
@shfshanyue shfshanyue added the code label Apr 4, 2024
@shfshanyue shfshanyue changed the title 【Q754】实现 Lazyman 【Q754】实现 LazyMan Apr 6, 2024
@HSIKE
Copy link

HSIKE commented May 27, 2024

function LazyMan(name) {
  console.log(`Hi! This is ${name}!`);
}
LazyMan.prototype.sleep = function(time) {
  const timeInMiliSec = time * 1000;
  const start = performance.now();
  console.log(`Wait for ${time} seconds...`);
  while (performance.now() - start <= timeInMiliSec) {}
  console.log(`Wake up after ${time} seconds.`);
  return this;
}
LazyMan.prototype.sleepFirst = function(time) {
  return this.sleep(time);
}
LazyMan.prototype.eat = function(thing) {
  console.log(`Eat ${thing}~~`);
  return this;
}

@shfshanyue
Copy link
Owner Author

function 方式

function LazyMan(name) {
  if (!(this instanceof LazyMan)) {
    return new LazyMan(name);
  }
  
  this.tasks = [];
  
  // 添加初始问候任务
  this.tasks.push(async () => {
    console.log(`Hi! This is ${name}!`);
  });

  // 开始执行任务队列
  this.runTasks();
  
  return this;
}

LazyMan.prototype.runTasks = async function() {
  setTimeout(async () => {
    for (const task of this.tasks) {
      await task();
    }
  }, 0);
};

LazyMan.prototype.sleep = function(seconds) {
  this.tasks.push(async () => {
    console.log(`等待${seconds}秒..`);
    await new Promise(resolve => setTimeout(resolve, seconds * 1000));
    console.log(`Wake up after ${seconds}`);
  });
  return this;
};

LazyMan.prototype.sleepFirst = function(seconds) {
  this.tasks.unshift(async () => {
    console.log(`等待${seconds}秒..`);
    await new Promise(resolve => setTimeout(resolve, seconds * 1000));
    console.log(`Wake up after ${seconds}`);
  });
  return this;
};

LazyMan.prototype.eat = function(food) {
  this.tasks.push(async () => {
    console.log(`Eat ${food}~`);
  });
  return this;
};

以及 class 方式

class LazyLazyMan {
  constructor(name) {
    
    this.tasks = [];
    
    // 添加初始问候任务
    this.tasks.push(async () => {
      console.log(`Hi! This is ${name}!`);
    });

    // 开始执行任务队列
    this.runTasks();
  }

  async runTasks() {
    setTimeout(async () => {
      for (const task of this.tasks) {
        await task();
      }
    }, 0);
  }

  sleep(seconds) {
    this.tasks.push(async () => {
      console.log(`等待${seconds}秒..`);
      await new Promise(resolve => setTimeout(resolve, seconds * 1000));
      console.log(`Wake up after ${seconds}`);
    });
    return this;
  }

  sleepFirst(seconds) {
    this.tasks.unshift(async () => {
      console.log(`等待${seconds}秒..`);
      await new Promise(resolve => setTimeout(resolve, seconds * 1000));
      console.log(`Wake up after ${seconds}`);
    });
    return this;
  }

  eat(food) {
    this.tasks.push(async () => {
      console.log(`Eat ${food}~`);
    });
    return this;
  }
}

function LazyMan(name) {
  return new LazyLazyMan(name);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants