Dart 角度2和函数$interval

Dart 角度2和函数$interval,dart,angular,carousel,settimeout,setinterval,Dart,Angular,Carousel,Settimeout,Setinterval,如何在Angular2中重写并在angular1中投射此代码: $interval(function () { if($scope.carouselIndex < $scope.showcases.length -1){ $scope.carouselIndex = $scope.carouselIndex + 1; } else { $scope.carouselIndex = 0; } }, properties.delay);

如何在Angular2中重写并在angular1中投射此代码:

$interval(function () {
    if($scope.carouselIndex < $scope.showcases.length -1){
      $scope.carouselIndex = $scope.carouselIndex + 1;
    } else {
      $scope.carouselIndex = 0;
    }

  }, properties.delay);
$interval(函数(){
如果($scope.carouselIndex<$scope.showcases.length-1){
$scope.carouselIndex=$scope.carouselIndex+1;
}否则{
$scope.carouselIndex=0;
}
},属性。延迟);
我试过这样做,但没有成功:

 carouselIndex = 0;
          for(int i=0; i<contents.size(); i++){
            if(carouselIndex < contents.length -1){
              setTimeout(function() {
              carouselIndex = carouselIndex + 1;
              }, 1000);
            } else {
              carouselIndex = 0;
            }
      }
carouselIndex=0;

对于(int i=0;i您可以尝试
可观察的间隔
方法:

Observable.interval(properties.delay).subscribe(() => {
  if (this.carouselIndex < this.showcases.length - 1) {
    this.carouselIndex = this.carouselIndex + 1;
  } else {
    this.carouselIndex = 0;
  }
});
Observable.interval(属性.延迟).订阅(()=>{
if(this.carouselIndex
每次达到延迟时都会触发一个事件。因此,将相应地调用订阅时定义的回调


在您的情况下,您尝试将同步循环与异步处理与
setTimeout
功能混合使用。

您可以尝试
可观察的方法。interval
方法:

Observable.interval(properties.delay).subscribe(() => {
  if (this.carouselIndex < this.showcases.length - 1) {
    this.carouselIndex = this.carouselIndex + 1;
  } else {
    this.carouselIndex = 0;
  }
});
Observable.interval(属性.延迟).订阅(()=>{
if(this.carouselIndex
每次达到延迟时都会触发一个事件。因此,将相应地调用订阅时定义的回调


在您的例子中,您尝试将同步循环与异步处理与
设置超时
功能混合使用。

您可以使用
定时器
类来实现此目的:

  Timer _timer;

  someFunc() {
    _timer = new Timer.periodic(const Duration(milliseconds: 1000 /*properties.delay*/),
        (_) {
      if (this.carouselIndex < this.showcases.length - 1) {
        this.carouselIndex = this.carouselIndex + 1;
      } else {
        this.carouselIndex = 0;
      }
    });

    // to stop the periodic execution use
    // _timer.cancel();
  }
Timer\u定时器;
someFunc(){
_计时器=新计时器。周期性(常量持续时间(毫秒:1000/*属性。延迟*/),
(_) {
if(this.carouselIndex
您可以使用
计时器
类执行以下操作:

  Timer _timer;

  someFunc() {
    _timer = new Timer.periodic(const Duration(milliseconds: 1000 /*properties.delay*/),
        (_) {
      if (this.carouselIndex < this.showcases.length - 1) {
        this.carouselIndex = this.carouselIndex + 1;
      } else {
        this.carouselIndex = 0;
      }
    });

    // to stop the periodic execution use
    // _timer.cancel();
  }
Timer\u定时器;
someFunc(){
_计时器=新计时器。周期性(常量持续时间(毫秒:1000/*属性。延迟*/),
(_) {
if(this.carouselIndex