// 利用setTimeout 实现setInterval let timer = null function interval(func, wait){ let subInterval = function(){ func.call(null) timer = setTimeout(subInterval,wait) } timer = setTimeout(subInterval,wait) } // 调用 interval(function() {}, 20) // 清除定时器 if(timer){ window.clearTimeout(timer) timer = null }