84. 创建一个fake timer (setInterval)
分享给更多人
中等难度 -通过 / -执行
这个题目是36. 实现一个 fake timer(setTimeout)的后续。
就像 setTimeout
,setInterval
也是不精确的(请参考Event Loop for detail)。
大多数情况下都不是大问题,但是在test的时候就不一样了。
你能否实现一个静态化的setInterval()
和clearInterval()
,而不再有Event Loop的问题。这也正是 FakeTimes 的用途。
“精确”的意思是,“假设所有的函数的执行耗时为0,同时时间戳从0开始,那么setInterval(func1, 100)将会精确的在以下时间执行func1: 100,200,300 ..."。
你需要同时修改Date.now()来提供新的时间戳。
class FakeTimer { install() { // replace window.setInterval, window.clearInterval, Date.now // with your implementation } uninstall() { // restore the original implementation of // window.setInterval, window.clearInterval, Date.now } tick() { // run the scheduled functions without waiting }}
** 请注意死循环 **,你的代码大概会这样被测试:
const fakeTimer = new FakeTimer()fakeTimer.install()const logs = []const log = () => { logs.push(Date.now())}let count = 0const id = setInterval(() => { if (count > 1) { clearInterval(id) } else { log() } count += 1}, 100)// log 'A' at every 100, stop at 200fakeTimer.tick()fakeTimer.uninstall() expect(logs).toEqual([100,200])
注意
测试的时候只会用到Date.now()
,其他的时间相关的函数可以忽略。