有没有办法可以动态地改变$ interval的频率

时间:2016-01-08 12:42:33

标签: angularjs

我想出了这个Typescript代码来检查连接性。它每隔60秒检查一次以确认连接。我想这样做,以便在连接丢失时更频繁地进行连接检查。我有办法做到这一点吗?我认为这意味着要改变时间间隔但是我不确定一旦$ interval启动并运行,该怎么做。

isConnected = (): ng.IPromise<any> => {
    return this.$http({
        method: 'GET',
        url: self.ac.dataServer + '/api/Connect/Verify',
        ignoreLoadingBar: true
    } as ng.IRequestConfig);
};

openConnectionHandler = () => {
    var self = this;
    self.minutes = 0;
    self.mos.closeConnectionModal()
}

closeConnectionHandler = () => {
    var self = this;
    if (self.minutes == 0) {
        self.connectionModalBody = "Unable to establish a connection to the " + self.shortDomainName + " server";
    } else if (self.minutes == 1) {
        self.connectionModalBody = "Unable to establish a connection to the " + self.shortDomainName + " server for " + self.minutes + " minute.";
    } else {
        self.connectionModalBody = "Unable to establish a connection to the " + self.shortDomainName + " server for " + self.minutes + " minutes.";
    }
    self.minutes++;
    self.mos.openConnectionModal();
}

checkConnection = () => {
    var self = this;
    self.isConnected().then(self.openConnectionHandler, self.closeConnectionHandler);
    this.$interval(function () {
        self.isConnected().then(self.openConnectionHandler, self.closeConnectionHandler);
    }, 60 * 1000);
}

在我的应用程序控制器中,我在应用程序启动时进行此调用:

uss.checkConnection();

2 个答案:

答案 0 :(得分:1)

是的,有办法。您取消当前间隔并创建一个具有不同超时值的新间隔。

示例:

//here is where your interval initialization
var intervalPromise = $interval(intervalWhenConnectionIsUp, 60*1000);

function intervalWhenConnectionIsUp(){
    //your stuff goes here

    //let's say there is a variable which we can use to identify connection failures
    if(connectionFailed){
        //just cancel the current interval by the `cancel` method
        intervalPromise.cancel();

        //and set the proper interval function
        intervalPromise = $interval(intervalWhenNoConnection, 30*1000)
    }
}

function intervalWhenNoConnection(){
    //your stuff goes here

    if(connectionIsUp){
        //just cancel the current interval by the `cancel` method
        intervalPromise.cancel();

        //and set the proper interval function
        intervalPromise = $interval(intervalWhenConnectionIsUp, 60*1000)

    }
}

答案 1 :(得分:1)

你可以在另一个函数中设置-interval,它将一个参数作为$ interval的超时。并以所需的时间间隔调用此函数。

这是没有全局变量的角度代码

function MyCtrl($scope, $interval) {
  var intervalInstance;

  function onNoConnection() {
    checkConnectionAtRate(30*1000);
  }

  function checkConnectionAtRate(rate) {
      if (angular.isDefined(intervalInstance)) {
         $interval.cancel(intercalInstance);
      }
      intervalInstance = $interval(function() {
       //here goes the code;
       console.log('Something');
      }, rate);
  }

   function checkConnection() {
     checkConnectionAtRate(60 * 1000);
   }

}

UPDATE 2以适合问题陈述(不确定打字稿语法)

  openConnectionHandler = () => {
      //same code
  }

  closeConnectionHandler = () => {
      //same code
      self.checkConnectionAt(30*1000);
  }

  checkConnection = () => {
      var self = this;
      self.checkConnectionAt(60 * 1000);
  }

  checkConnectionAt = (rate) => {
      var self = this;

      if(self.intervalInstance) {
          $interval.cancel(self.intervalInstance);
      }

      self.intervalInstance = $interval(function () {
          self.isConnected().then(self.openConnectionHandler, self.closeConnectionHandler);
      }, rate);
  }