gpt4 book ai didi

javascript - 即使关闭指令后服务也不会停止

转载 作者:行者123 更新时间:2023-12-03 06:36:22 25 4
gpt4 key购买 nike

我有一个模态窗口,其中有一个指令,我在其中调用一个服务,该服务每隔 30 秒在 $interval 内发出一次 http 请求,但是当我关闭模态窗口时,该服务仍然在每 30 秒后运行30秒,有没有办法可以在关闭模态窗口的同时停止它?

最佳答案

Angular 包裹的 interval 应显式销毁,如文档中所述:

Intervals created by this service must be explicitly destroyed when you are finished with them. In particular they are not automatically destroyed when a controller's scope or a directive's element are destroyed.

您应该在删除指令时取消间隔,如下所示:

var stop = $interval(myFunc, 1000)

$scope.on('$destroy', () => {
$interval.cancel(stop);
});

关于javascript - 即使关闭指令后服务也不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38183064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com