javascript 中的 setTimeout 和 angularjs 中的 $timeout 服务之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39310992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:27:20  来源:igfitidea点击:

difference between setTimeout in javascript and $timeout service in angularjs

javascriptangularjssettimeoutangular-services

提问by Isetty Ravitejakumar

Iam new to angular framework.Here is my scenario where, I want to change my $scope.variable after a period of time so i used javascript setTimeoutmethod.

我是角度框架的新手。这是我的场景,我想在一段时间后更改我的 $scope.variable,所以我使用了 javascriptsetTimeout方法。

$scope.variable = 'Previous';

setTimeout(function(){
  $scope.variable='NEXT';
},3000);

This code doesn't work for me. I used $apply()to make this code work.

这段代码对我不起作用。我曾经$apply()让这段代码工作。

Later I came to know that angular itself has a $timeout service which does the same work.

后来我才知道 angular 本身有一个 $timeout 服务,它做同样的工作。

$scope.variable = 'Previous';

$timeout(function () {
  $scope.variable = 'NEXT';
}, 2000);

How can i compare performance of $timeoutservice with javascript setTimeout??

如何将$timeout服务性能与 javascript进行比较setTimeout

Why we should use $timeoutinstead of setTimeout??

为什么我们应该使用$timeout而不是setTimeout??

Please give me some examples and reasons to use it, which shows the performance.

请给我一些例子和使用它的理由,它显示了性能。

Thanks :)

谢谢 :)

采纳答案by Nikhilesh Shivarathri

There are some cases where one needs to perform some sort of timeout operation and we frequently achieve this using JavaScript's setTimeout()function.

在某些情况下,需要执行某种超时操作,我们经常使用 JavaScript 的setTimeout()函数来实现这一点。

However, if we use setTimeout()in an AngularJS application we also need to use $scope.$apply()to ensure that any changes to the scopewill be reflected elsewhere (i.e. data-bound in a view).

但是,如果我们setTimeout()在 AngularJS 应用程序中使用,我们还需要使用$scope.$apply()来确保对 的任何更改scope都将反映在其他地方(即 a 中的数据绑定view)。

AngularJSprovides a handy wrapper for this: $timeout()- it does the $apply()for which we don't have to $applythe changes.

AngularJS为此提供了一个方便的包装器:$timeout()- 它执行$apply()我们不需要$apply更改的内容。

Regarding the performance.

关于性能

If you're using $timeoutto create what is essentially an interval, then don't use it. If your application is large then $applywill also trigger a $digestcycle which you may not really want it to happen, it will surely decrease the performance.

如果您要$timeout创建本质上是间隔的内容,请不要使用它。如果您的应用程序很大,那么$apply也会触发一个$digest您可能并不真正希望它发生的循环,它肯定会降低性能。

回答by Tarun

Any AngularJS scope variable when handled from outside (including ajax) needs a $apply().

从外部(包括 ajax)处理的任何 AngularJS 范围变量都需要 $apply()。

$timeout() takes care of the current scope and runs in the same digest cycle after all the change detection is done.

$timeout() 负责处理当前范围,并在所有更改检测完成后在同一个摘要周期中运行。

One beauty of $timeout() that I recently discovered is, if you do not pass the time parameter, it will wait for the DOM completion.

我最近发现的 $timeout() 的一个优点是,如果您不传递时间参数,它将等待 DOM 完成。

So,

所以,

$timeout(function(){
  console.log("show after directive partial loaded")
}); //note, no time parameter

Run this code in a directive and the timeout callback function will be fired once the partial has been loaded by the directive

在指令中运行此代码,一旦指令加载了部分,就会触发超时回调函数

Hope this helps.

希望这可以帮助。