heavy r.com index.php,AngularJS - Computation-Heavy Tasks

问题

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值