Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

setTimeout准时

Sunny-117 opened this issue · comments

commented
setTimeout准时
commented
function setTimeoutWithOffset(fn, interval, ...args) {
  let startTime = Date.now()
  let count = 0// 执行次数
  let timer = null
  const task = () => {
    // offset = timeNow - (startTime + count * interval)
    const offset = Date.now() - (startTime + count * interval)
    console.log(`第${count + 1}次 系统延迟时间${offset}`)//
    timer = setTimeout(() => {
      fn.apply(this, args);
      count++
      task();
    }, interval - offset);
  };
  task();
  return () => clearTimeout(timer);
}

//TEST
const stop = setTimeoutWithOffset(() => console.log(123), 1000)
setTimeout(() => {
  stop()
}, 5000);