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

我们能反过来使用 setinterval 模拟实现 settimeout 吗?

Sunny-117 opened this issue · comments

我们能反过来使用 setinterval 模拟实现 settimeout 吗?
function _setTimeout(fn, delay, ...args) {
  const timer = setInterval(() => {
    fn.apply(this, args);
    clearInterval(timer);
  },delay)
}
commented

function timeout(fn, delay = 0, ...args) {
let timer = setInterval(() => {
fn(...args);
clearInterval(timer);
}, delay);
}