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

使用 setTimeout 实现 setInterval

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

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

function _setInterval(fn,delay=4,...args) {
  let cancel = false;
  const task = () => {
    setTimeout(() => {
      if (!cancel) {
        fn.apply(this, args);
        task();
      }
    },delay)
  }
  task();
  return () => { cancel = true };
}

CwRv07 avatar Nov 21 '22 07:11 CwRv07

function interval(callback, time) { let inter = () => { setTimeout(() => { callback(); inter(); }, time); }; inter(); } interval(() => { console.log(每隔1秒输出); }, 1000);

hyxieshi avatar Jan 13 '23 14:01 hyxieshi


let timer = null;
function myInterval(cb, delay) {
    let interval = () => {
        cb();
        timer = setTimeout(interval, delay);  // 递归执行
    }
    timer = setTimeout(interval, delay); //触发执行
}
myInterval(() => { console.log('I am Jack') }, 1000)

LifeIsTerrible avatar Feb 26 '23 14:02 LifeIsTerrible

function mySetInterval(func, delay, ...args) {
  let timer = null
  function fun() {
    return setTimeout(() => {
      func(...args)
      timer = fun()
    }, delay)
  }
  timer = fun()
  return () => { clearTimeout(timer) }
}

let clear = mySetInterval(() => {
  console.log(11);
}, 1000)

setTimeout(() => {
  clear()
}, 2100)

nanyishixiong avatar Mar 14 '23 11:03 nanyishixiong

let timeWorker = {}
let _setInterval = function (fn, time,...args) {
  // 定义一个key,来标识此定时器
  let key = Symbol();
  // 定义一个递归函数,持续调用定时器
  let execute = function (fn, time) {
    timeWorker[key] = setTimeout(function () {
      fn(...args);
      execute(fn, time);
    }, time)
  }
  execute(fn, time);
  // 返回key
  return key;
}
let _clearInterval = function (key) {
  if (key in timeWorker) {
    clearTimeout(timeWorker[key]);
    delete timeWorker[key];
  }
}
// test
!(() => {
  let timer = _setInterval(() => console.log(1), 1000)
  setTimeout(() => {
    _clearInterval(timer)
  }, 10000)
})()

2239351258 avatar Mar 28 '23 09:03 2239351258

function interval(fn, delay = 0, ...args) { setTimeout(() => { fn(...args); interval(fn, delay, ...args); }, delay); }

cscty avatar Jun 18 '23 13:06 cscty

function mySetInterval(fn, delay, ...args) {
  let timer = null;
  const task = () => {
    timer = setTimeout(() => {
      if (timer) {
        fn.apply(this, args);
        task();
      }
    }, delay);
  }
  task();
  return () => {
    clearTimeout(timer);
    timer = null;
  }
}

const cancel = mySetInterval(console.log, 1000, 'mySetInterval')

setTimeout(() => {
  cancel();
}, 4500)

IhInspiration avatar Jul 14 '23 02:07 IhInspiration

function mySetInterval(cb,time){
    setTimeout(() => {
        cb()
        mySetInterval(cb,time)
    },time)
}
mySetInterval(() => {
    console.log('hello')
},1000)

jianxingyao avatar Sep 15 '24 12:09 jianxingyao

function myInterval(callback, delay) {
    // callback() //放到这里立即执行一次再延时执行
    const timer = setTimeout(() => {
        callback()
        clearTimeout(timer)
        myInterval(callback, delay) 
    }, delay)
}
myInterval(() => {
    console.log('Interval')
},1000)

Payhoww avatar Jun 23 '25 08:06 Payhoww