setInterval实现

// setTimeout模拟setInterval
const _setInterval = (fn, t) => {
            function tx() {
                setTimeout(tx, t)
                fn()
            }
            setTimeout(tx, t);
        }
        _setInterval(() => { console.log('2') }, 1000)
// 兼容IE的requestAnimationFrame 
requestAnimationFrame = function (callback) {
      const currTime = new Date().getTime()
      // 为了使setTimteout的尽可能的接近每秒60帧的效果
      const timeToCall = Math.max(0, 16 - (currTime - lastTime))
      const id = window.setTimeout(() => {
        callback(currTime + timeToCall)
      }, timeToCall)
      lastTime = currTime + timeToCall
      return id
    }

你可能感兴趣的:(setInterval实现)