纯CSS旋转器冻结
Pure CSS spinner freezes
我有一个纯CSS微调器,我想显示,而我的JavaScript正在做其他工作(异步调用,然后迭代通过大约10,000个元素的数组)。旋转器会在正确的时间出现,但当触及代码中的某个点时就会冻结。在下面的代码中,"1"、"2"answers"3"的console.log
s基本上是立即发生的——然后有一个很长的暂停(这是我的旋转器冻结的时候),然后"4"answers"5"一起退出,我的内容加载。所以基本上我知道" console.log('three')
"answers" console.log('four')
"之间的代码:
// for search bar
search(text) {
this.searching = true; // shows spinner
console.log('one');
var text = text.toUpperCase();
// search the text with an api call
this.securitiesService.searchSecurities(text)
.subscribe(data => {
var response = data.response;
console.log('two');
// if no search results are present go to correct page
if (!response.length) {
this.router.navigate(...to route...);
return;
}
console.log('three');
// FREEZES HERE !!!
for (var i = 0; i < response.length; i++) {
if (text === response[i].ticker) {
// UNFREEZES HERE !!!
console.log('four');
this.router.navigate(...to route...);
this.searching = false;
console.log('five');
return;
}
}
})
}
我尝试过的每个纯CSS微调器都会出现这种情况。
我认为我的JavaScript不应该冻结我的纯CSS旋转器是错误的吗?为什么代码会冻结它?
JavaScript在UI线程上运行,因此它仍然会阻止仅用于css的旋转器。你需要将工作移出UI线程,或者将其分开,以便UI有时间更新。
根据你需要支持的浏览器,你可以使用Web Workers。请参阅http://caniuse.com/#search=web%20workers获取浏览器支持,或参阅http://www.html5hacks.com/blog/2012/09/22/web-workers-basics-of-the-web-browsers-ui-thread/获取示例。
您还可以一次处理一条(或10条或100条)记录,并将window.setTimeout
或window.requestAnimationFrame
中的每个记录称为"批处理"。试试这样做(代码未测试):
// for search bar
search(text) {
this.searching = true; // shows spinner
console.log('one');
var text = text.toUpperCase();
// search the text with an api call
this.securitiesService.searchSecurities(text)
.subscribe(data => {
var response = data.response;
console.log('two');
// if no search results are present go to correct page
if (!response.length) {
this.router.navigate(...to route...);
return;
}
console.log('three');
var i = 0;
var self = this;
var processBatch = function () {
if (text === response[i].ticker) {
console.log('four');
self.router.navigate(...to route...);
self.searching = false;
console.log('five');
return;
}
if (++i < response.length) {
window.setTimeout(processBatch, 0);
}
};
window.setTimeout(processBatch, 0);
});
}
如果你想在一个批处理中处理多个项目,你可以在processBatch
中设置一个循环,迭代少量的项目。
旁注:如果data.response
是字符串,请参阅@gelliott181上面的评论
- 在哪里使用名为“;冻结”;
- 知道为什么我的旋转木马不会自动更改图片吗
- For循环冻结Javascript
- 顺时针旋转Svg元件
- 将鼠标旋转限制为特定的度数
- 旋转后拖动对象
- 禁用旋转木马中的下一个按钮和上一个按钮
- 为什么继续;语句冻结浏览器
- 手动创建旋转活动指示器
- 如何在容器中定位旋转的图像
- 在轴上旋转SVG图像
- 绕z轴旋转时出现iOS陀螺仪错误
- 旋转按钮[HTML][JS]
- Javascript-SetTimeout导致浏览器冻结
- 用于发现哪个JS/jQuery脚本正在冻结页面的工具或技术
- 在JS中围绕旋转对象旋转对象
- Threejs-围绕中心旋转对象
- 加载记录时旋转图标冻结
- 简单的窗口阴影/冻结之间的注册步骤与旋转器
- 纯CSS旋转器冻结