我如何使用在 Angular 中使用的$timeout和$interval.js但对于 Angular 版本?



在Angular中.js我使用了$timeout$interval(类似于javascript中的setIntervalsetTimeout(。

$timeout(function(){})
$interval(function(){},5000)

取消我使用的间隔

$interval.cancel($scope.interval); 

当离开视图并避免$interval仍在运行时,我使用了。我是Angular的新人。

$scope.$on('$ destroy', function () {
$interval.cancel($scope.interval)
})

我如何使用我在角度中使用的这两个函数的等效.js到角度?

像在 Angular 中一样使用setIntervalsetTimeout,但在组件实例减少之前使用ngOnDestroy生命周期钩子来清除计时器的内容。这个生命周期钩子有助于清除东西,它与 angularjs$destroy事件类似。

ngOnDestroy(){
//do clear intervals and timeouts here
//clearInterval(yourInterval);
//clearTimeout(yourTimeout);
}

使用 setTimeout 本机函数。角度中没有特殊的服务来处理计时器。

最新更新