如何实现停止并开始使用$ interval服务

时间:2022-03-26 19:31:36

I use $interval in my project.

我在我的项目中使用$ interval。

here is code from my controller:

这是来自我的控制器的代码:

self.timer = $interval(function () {
        checkUpdate();
    }, 10000, false);

function checkUpdate() {
    sensorsDataServise.checkForUpdate("2012-10-04T16:10:00")
    .then(function (result) {
        if (result.status == '200') {
            //here to stop the $interval
            return result.data;
        }
    });
}

function modalClosed() {
    //here to start $interval
}

I need to stop and start $interval service.

我需要停止并启动$ interval服务。

I try to use this $interval.cancel(self.timer) in checkUpdate function and

我尝试在checkUpdate函数中使用这个$ interval.cancel(self.timer)

I try to use this $interval.start(self.timer) in modalClosed function.

我尝试在modalClosed函数中使用这个$ interval.start(self.timer)。

But I get error:

但我得到错误:

$interval.cancel and $interval.start is not a function.

$ interval.cancel和$ interval.start不是函数。

How can I implement stop and start using $interval service.

如何实现停止并开始使用$ interval服务。

2 个解决方案

#1


3  

I would say : RTFM -> https://docs.angularjs.org/api/ng/service/$interval

我会说:RTFM - > https://docs.angularjs.org/api/ng/service/$interval

More precisely : to start you don't need .start just do

更确切地说:开始你不需要.start就是这样

var stopHandler = $interval(myFn);

To stop do :

要停止做:

$interval.cancel(stopHandler);

The fact that $interval.cancel seems to not be defined in your sample code may be due to a injection problem, show us your controller/service definition that goes along with the code you showed us

$ interval.cancel似乎未在您的示例代码中定义的事实可能是由于注入问题,请向我们展示您的控制器/服务定义以及您向我们展示的代码

#2


1  

$interval does have a .cancel method which cancels the current Timer but there is no method called start.

$ interval确实有一个取消当前Timer的.cancel方法,但没有一个叫做start的方法。

to start a timer, you just need to run the $internal inside the modalClosed and clear the same timer inside checkUpdate().

要启动一个计时器,你只需要在modalClosed中运行$ internal并清除checkUpdate()中的相同计时器。

Note:Make sure ModalClosed() function is executed first and then checkUpdate() else self.timer object will be undefined.

注意:确保首先执行ModalClosed()函数,然后检查checkUpdate()else self.timer对象是否未定义。

JS CODE:

JS代码:

var self = this;

function checkUpdate() {
        sensorsDataServise.checkForUpdate("2012-10-04T16:10:00")
       .then(function (result) {
           if (result.status == '200') {
              //here to stop the $interval
              $internal.cancel(self.timer);
               return result.data;
           }
       });
    }



function modalClosed () {
   //here to start $interval
   self.timer = $interval(function () { checkUpdate(); }, 10000, false);
}

#1


3  

I would say : RTFM -> https://docs.angularjs.org/api/ng/service/$interval

我会说:RTFM - > https://docs.angularjs.org/api/ng/service/$interval

More precisely : to start you don't need .start just do

更确切地说:开始你不需要.start就是这样

var stopHandler = $interval(myFn);

To stop do :

要停止做:

$interval.cancel(stopHandler);

The fact that $interval.cancel seems to not be defined in your sample code may be due to a injection problem, show us your controller/service definition that goes along with the code you showed us

$ interval.cancel似乎未在您的示例代码中定义的事实可能是由于注入问题,请向我们展示您的控制器/服务定义以及您向我们展示的代码

#2


1  

$interval does have a .cancel method which cancels the current Timer but there is no method called start.

$ interval确实有一个取消当前Timer的.cancel方法,但没有一个叫做start的方法。

to start a timer, you just need to run the $internal inside the modalClosed and clear the same timer inside checkUpdate().

要启动一个计时器,你只需要在modalClosed中运行$ internal并清除checkUpdate()中的相同计时器。

Note:Make sure ModalClosed() function is executed first and then checkUpdate() else self.timer object will be undefined.

注意:确保首先执行ModalClosed()函数,然后检查checkUpdate()else self.timer对象是否未定义。

JS CODE:

JS代码:

var self = this;

function checkUpdate() {
        sensorsDataServise.checkForUpdate("2012-10-04T16:10:00")
       .then(function (result) {
           if (result.status == '200') {
              //here to stop the $interval
              $internal.cancel(self.timer);
               return result.data;
           }
       });
    }



function modalClosed () {
   //here to start $interval
   self.timer = $interval(function () { checkUpdate(); }, 10000, false);
}