并发Angularjs [英] Concurrency in Angularjs

查看:154
本文介绍了并发Angularjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角度应用程序,并使用大量$ http.get异步调用,我在我的范围($ scope.count)的计数变量,跟踪我多少请求作出多少仍悬而未决。很显然,我使用它像这样:

I have an angular app that does lots of asynchronous calls using $http.get, and I have a count variable in my scope ($scope.count) that keeps track of how many requests I made and how many are still pending. Obviously, I'm using it like so:


  • 之前,我做一个$ http.get要求我增加了1
  • 计数
  • 当我从$ http.get我1
  • 递减计数响应
  • Before I make a $http.get request I increment the count by 1
  • When I get a response from the $http.get I decrement the count by 1

我做很多的请求,2000年左右的请求的东西都在同一时间,但$ scope.count值不回0所有请求完成后,甚至,它总是大于0(总是关闭的1或2)。我在处理成功和我的$ http.get调用错误事件,我递减计数,只要其中一人发生了。

I'm making lots of requests, something around 2000 requests all at the same time, but the $scope.count value is not going back to 0 even after all requests are done, it's always greater than 0 (always off by 1 or 2). I'm handling both success and error events for my $http.get call, and I decrement the count whenever one of them happens.

所以我在想,如果角/ JavaScript的处理并发呢?我想我跑了递增操作这么多次(在同一时间许多潜在的)和$ scope.count的价值变得过时/坏,因为两个操作可以修改在同一时间变量。

So I was wondering if angular/javascript handle concurrency well? I'm thinking I'm running the increment operation so many times (potentially many at the same time) and the value of $scope.count becomes obsolete/bad since two operations can be modifying the variable at the same time.

推荐答案

JavaScript执行单线程(事件循环),从而并发问题是不可能的。

Javascript runs single threaded (an event loop) so the concurrency problem is not possible.

你应该尝试做的是使用拦截器。文档有一个伟大的例如

What you should try to do is use an interceptor. The documentation has a great example.

您可以把您的计数的 $ rootScope

这篇关于并发Angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆