dart - Angular 2和函数$ interval

标签 dart angular carousel settimeout setinterval

我如何在Angular2和Dart中重写angular1中的代码:

$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;
            }
      }

任何的想法?
谢谢

最佳答案

您可以尝试Observable.interval方法:

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

每次达到延迟都会触发一个事件。因此,在订阅时定义的回调将被相应地调用。

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

关于dart - Angular 2和函数$ interval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36397535/

相关文章:

unit-testing - 在 Dart 中,如何在单元测试中验证不抛出异常的 void 方法?

android - 如何解决这个 "Cannot enable MyLocation layer as location permissions are not granted"?

dart - 如何从 Future 返回 Future?或者这在异步库中是禁止的?

node.js - 是什么原因导致 localhost :4200 to be added in front of route call?

javascript - 如何在 Angular 2 中使用 vanilla js/es6 的 bootstrap?

firebase - 如何访问Future方法中存在的回调函数的返回值。 Dart

javascript - 如何仅在 Swiper JS 中为中间幻灯片启用中心幻灯片?

html - Bootstrap 轮播出现问题 : images won't resize according to container size

angularjs - UI 引导轮播第一张幻灯片不显示,然后在最后一张幻灯片崩溃

Angular 5 响应式(Reactive)表单模式验证器