在Angular中.js我使用了$timeout
和$interval
(类似于javascript中的setInterval
和setTimeout
(。
$timeout(function(){})
$interval(function(){},5000)
取消我使用的间隔
$interval.cancel($scope.interval);
当离开视图并避免$interval
仍在运行时,我使用了。我是Angular的新人。
$scope.$on('$ destroy', function () {
$interval.cancel($scope.interval)
})
我如何使用我在角度中使用的这两个函数的等效.js到角度?
像在 Angular 中一样使用setInterval
和setTimeout
,但在组件实例减少之前使用ngOnDestroy
生命周期钩子来清除计时器的内容。这个生命周期钩子有助于清除东西,它与 angularjs$destroy
事件类似。
ngOnDestroy(){
//do clear intervals and timeouts here
//clearInterval(yourInterval);
//clearTimeout(yourTimeout);
}
使用 setTimeout 本机函数。角度中没有特殊的服务来处理计时器。