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

使用 setTimeout 实现 setInterval #156

Open
Sunny-117 opened this issue Nov 3, 2022 · 8 comments
Open

使用 setTimeout 实现 setInterval #156

Sunny-117 opened this issue Nov 3, 2022 · 8 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@CwRv07
Copy link
Contributor

CwRv07 commented Nov 21, 2022

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

@hyxieshi
Copy link

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

@LifeIsTerrible
Copy link


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)

@nanyishixiong
Copy link

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)

@2239351258
Copy link

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)
})()

@cscty
Copy link

cscty commented Jun 18, 2023

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

@IhInspiration
Copy link

IhInspiration commented Jul 14, 2023

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)

@jianxingyao
Copy link

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

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

No branches or pull requests

9 participants