javascript - 为什么 history.js 使用 setInterval(.., 250)?

标签 javascript performance setinterval history.js html5-history

据我了解History.jsHTML5 History/State APIs 的 polyfill .因此,在现代浏览器上,它应该简单地使用 popstate用于收听 URL 更改。那么,为什么在最新的 Chrome 上我可以看到计时器每 250 毫秒触发一次?这看起来很浪费,每隔几秒就会触发一次垃圾收集器。

看看这个官方History.js demo .

History.js demo page firing timer every 250ms

最佳答案

我不知道 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/

相关文章:

javascript - ngHide 指令仅在页面刷新后适用于 ngRoute 模块

c++ - 纯 C++ 代码比内联汇编程序快 10 倍。为什么?

javascript - 在 setInterval 函数中传递参数

javascript 重定向仅在显示 IE 开发控制台后在 IE 中起作用

javascript - 将文本替换为 HTML 文件中的 html 标签

java - 从 Python 运行 Java jar 文件的最快方法?

javascript - 为什么 Firefox setInterval 回调参数与其他浏览器不同?

javascript - 如何在单击时停止 setInterval 函数,并在几秒钟后重新启动

javascript - 以编程方式 react 设置组件 Prop

sql-server - 如何优化运行数百万条记录的 SQL Server Merge 语句