问题

Is there a pattern in Angular apps for computation-heavy tasks? (Beyond just using $timeout with a delay of 0 to let them get off the call stack?) Even using $timeout seems to make the UI unresponsive when the processing itself kicks in. Do I need to be looking at Web Workers or something like that, or is there an "Angular way?"

回答1:

Because JavaScript is single threaded, you need to either do the computations server-side, or do the timeouts in between the processing (See underscore's defer(), http://underscorejs.org/#defer). Otherwise, the UI will inevitably get blocked.

回答2:

I came up with a way to solve my UI responsiveness problem by creating special loop functions that use an Angular $timeout each time through the loop.

app.service('responsivenessService', function($q, $timeout) {

var self = this;

// Works like Underscore's map() except it uses setTimeout between each loop iteration

// to try to keep the UI as responsive as possible

self.responsiveMap = function(collection, evalFn)

{

var deferred = $q.defer();

// Closures to track the resulting collection as it's built and the iteration index

var resultCollection = [], index = 0;

function enQueueNext() {

$timeout(function () {

// Process the element at "index"

resultCollection.push(evalFn(collection[index]));

index++;

if (index < collection.length)

enQueueNext();

else

{

// We're done; resolve the promise

deferred.resolve(resultCollection);

}

}, 0);

}

// Start off the process

enQueueNext();

return deferred.promise;

}

return self;

});

This mapping function returns a promise which can be assignable to a $scope. Usage is similar to the map() functions from Underscore or native Javascript arrays in newer browsers:

$scope.results = responsivenessService.responsiveMap(initialdata, function() {

// Long-running or computationally intense code here

return result;

});

Code that would originally have blocked the whole UI now appears to run in the background (although essentially it's an illusion, like calling Application.DoEvents periodically in older applications). Nice and generic, as long as the long-running code is conducive to a looping map()-style operation!

来源:https://stackoverflow.com/questions/17299715/angularjs-computation-heavy-tasks

heavy r.com index.php,AngularJS - Computation-Heavy Tasks相关推荐

  1. 在PowerBI中使用R的限制

    已知限制 Power BI服务中的R视觉有一些限制: R视觉支持仅限于下一页中标识的软件包 .目前不支持定制软件包. 数据大小限制 - R视图用于绘图的数据限制为15万行.如果选择了超过15万行,则仅 ...

  2. [译]用AngularJS构建大型ASP.NET单页应用(二)

    原文地址:http://www.codeproject.com/Articles/808213/Developing-a-Large-Scale-Application-with-a-Single 客 ...

  3. 如何在vs中创建r树索引代码_线段树详解与实现

    此篇文章用于记录<玩转数据结构>课程的学习笔记 什么是线段树 线段树也被称为区间树,英文名为Segment Tree或者Interval tree,是一种高级的数据结构.这种数据结构更多出 ...

  4. 【翻译】Awesome R资源大全中文版来了,全球最火的R工具包一网打尽,超过300+工具,还在等什么?...

    0.前言 虽然很早就知道R被微软收购,也很早知道R在统计分析处理方面很强大,开始一直没有行动过...直到 直到12月初在微软技术大会,看到我软的工程师演示R的使用,我就震惊了,然后最近在网上到处了解和 ...

  5. 对于一些常用的R语言的算法包的归纳(修改)

    基于R,仅供自食. 相信自己,每天多学一点. (part2来源:https://blog.csdn.net/LW_GHY/article/details/56501063) part1: 连续因变量的 ...

  6. sheet.range(‘U‘ + str(index - 1), ‘U‘ + str(index)).api.merge(),xlwings合并Excel上下相邻单元格,代码中断执行,也不报错。

    一.背景 采用xlwings包,根据template.xlsx模板,导出excel成果内容,包括多个sheet内容的填充,可实现上下相邻单元格的合并(左右单元格合并也是一个道理),代码运行卡着,也不报 ...

  7. android evaluater_android – 带有test.R.java的Robolectric

    我在API21上有一个使用robolectric 3.0的库项目,com.android.tools.build:grad:1.3.1. 我想在robolectric测试中使用测试资源(好像在src ...

  8. python 修改计算机名_静心学数据分析002-python基础

    1.前言 在安装好miniconda后,可以开始<learn python3 the hard way--a very simple introduction to the terrifying ...

  9. Android10.0 BroadcastCast广播机制原理

    原文地址:https://skytoby.github.io/2019/BroadcastCast%E5%B9%BF%E6%92%AD%E6%9C%BA%E5%88%B6%E5%8E%9F%E7%90 ...

最新文章

  1. 获得服务器硬件信息(CPUID、硬盘号、主板序列号、IP地址等)
  2. qiankun 微前端_基于qiankun落地部署微前端爬”坑“记
  3. python实现简单的api接口-使用Python编写API接口和使用API接口
  4. 在Linux下和Windows下遍历目录的方法及如何达成一致性操作
  5. 001_汽车之家,新浪和360之间的交流
  6. 骗人的数学题,那消失的1块钱到底被谁拿走了
  7. 计算机版初中语文课文原文,《背影》课文原文
  8. 关于原生AJAX和jQueryAJAX的编程
  9. 对Oracle SQL Developer中 变量的学习
  10. 操作系统就是虚拟机--主内又主外
  11. android银行卡号扫描二维码,支付宝扫描银行卡号识别SDK
  12. 二叉树模型matlab实现,利用Matlab实现二叉树的树形显示
  13. unity 三消游戏
  14. 免费网站地图制作工具 —— Sitemap X
  15. 【网络设备】H3C FW V7:安全域与域间策略
  16. 给文字上加中划线_Word中为字符添加上划线该怎么做
  17. 打开xshell一直没反应,xshell打开失败的问题
  18. dw常用标签_一个新人对于DW标签的理解
  19. LaTeX 中使用三级标题
  20. 从0开始写前端UI框架:概述

热门文章

  1. win10自动修复无法关闭的解决方案
  2. MVG读书笔记——几何变换续
  3. Android和C#实现实时视频传输Demo
  4. 【计算机网络】第三部分 数据链路层(15) 连接局域网、主干网和虚拟局域网
  5. 百度贴吧五年内流失九成用户;罗永浩吐槽苹果新品:更丑更贵更胡来;进互联网大厂毕业生5年后7成人离开 | EA周报...
  6. JavaWEB做一个美女网站
  7. Arduino 实时时钟DS1302模块
  8. 2017.12.5 八周第二次课
  9. 小程序 uni canvas绘制圆角图片 圆角矩形
  10. 第十周 项目一 计算税后收入