2016-04-04 41 views
1
Ben Angular2 ve Dart içinde angular1 Bu kodu yeniden yazabilirsiniz nasıl

: Ben bu şekilde denedim ama işe yaramadı

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

    }, properties.delay); 

:

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

Herhangi bir fikir? Teşekkür

cevap

0

Sen Observable.interval yöntemi deneyebilirsiniz:

Observable.interval(properties.delay).subscribe(() => { 
    if (this.carouselIndex < this.showcases.length - 1) { 
    this.carouselIndex = this.carouselIndex + 1; 
    } else { 
    this.carouselIndex = 0; 
    } 
}); 

bir olay süresine ulaşıldığında her zaman tetiklenir. Dolayısıyla, abonelik uygun olduğunda geri aranacak.

Sizin örneğinizde, setTimeout işleviyle eşzamanlı olmayan bir işlemle eşzamanlı bir döngü karıştırmayı denediniz.

0

Bunun için Timer sınıfını kullanabilirsiniz:

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(); 
    }