据我了解History.js是 HTML5 History/State APIs 的 polyfill .因此,在现代浏览器上,它应该简单地使用 popstate用于收听 URL 更改。那么,为什么在最新的 Chrome 上我可以看到计时器每 250 毫秒触发一次?这看起来很浪费,每隔几秒就会触发一次垃圾收集器。
看看这个官方History.js demo .
最佳答案
我不知道 History.js 代码,但通读它,我相信 @apsillers 是正确的。
它被配置为队列项之间的间隔为 250 毫秒。该队列中的一些排队函数需要获取历史状态。这些项目通过调用繁忙来强制冲洗暂停。
至于为什么这种情况也会发生在 HTML5 浏览器中,对我来说似乎是 History.JS 如果它是 HTML5 则不会只是切换一个开关然后什么都不做。在他们的 github它陈述了他们的目标之一:
Provide a cross-compatible experience for all HTML5 Browsers (they all implement the HTML5 History API a little bit differently causing different behaviours and sometimes bugs - History.js fixes this ensuring the experience is as expected / the same / great throughout the HTML5 browsers)
如果 History.js 仍在做一些涉及排队的工作,我猜他们会使用忙碌超时。
关于javascript - 为什么 history.js 使用 setInterval(.., 250)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17696953/