【问题标题】:How to change Background image every x amount of seconds如何每 x 秒更改一次背景图像
【发布时间】:2021-03-16 15:21:50
【问题描述】:

我对 Angular 和一般编程很陌生。

我想使用setInterval 方法更改我的主页的背景图片。它应该每秒都在变化,但由于某种原因,它变化得更快。

组件:

export class AppComponent implements OnInit {
  images: Image[] = [];
  changeBackgroundCounter = 0;
  constructor(private imagesService: ImagesService) {}

  getImage() {
    setInterval(() => {
      this.changeBackgroundCounter = this.changeBackgroundCounter + 1;
      if (this.changeBackgroundCounter > this.images.length - 1) {
        this.changeBackgroundCounter = 0;
      }
    }, 1000);
    return this.images[this.changeBackgroundCounter].image;
  }
  ngOnInit() {
    this.images = this.imagesService.getImages();
    console.log(this.images[0]);
  }
}

模板:

<div [ngStyle]="{'background-image': 'url('+ getImage() + ')'}" [ngClass]="{imageBackground: getImage()}">

Stackblitz link

【问题讨论】:

  • getImage 被调用了很多次,所以你正在创建大量的间隔

标签: html angular typescript setinterval


【解决方案1】:

在您的模板中,您有

<div [ngStyle]="{'background-image': 'url('+ getImage() + ')'}" [ngClass]="{imageBackground: getImage()}">

这意味着 Angular 不断调用getImage() 方法来找出背景应该是什么。这将非常频繁地发生。每次调用该方法时,都会创建一个新间隔,因此最终会加载它们。您可以通过在您的时间间隔内放置一行日志来查看这一点,您将看到它被触发的频率。

setInterval(() => {
  console.log('interval triggered'); // <------- add this line to see how often this code is running
  this.changeBackgroundCounter = this.changeBackgroundCounter + 1;
  if (this.changeBackgroundCounter > this.images.length - 1) {
    this.changeBackgroundCounter = 0;
  }
}, 1000);

要解决您的问题,您只需致电getImage() 一次,即可在ngOnInit() 内完成。模板可以从images[this.changeBackgroundCounter].image获取图片。

【讨论】:

    【解决方案2】:

    你让你的代码变得复杂是徒劳的。创建一个等于字符串的变量,并在您的 ngOnInit() 中每隔 X 秒为其分配一个新值! 然后将背景图像设置为等于该变量,瞧!

    这是它在代码中的样子:

    export class AppComponent implements OnInit {
      images: Image[] = [];
      actualImage: string;
      changeBackgroundCounter = 0;
      constructor(private imagesService: ImagesService) {}
    
      ngOnInit() {
        this.images = this.imagesService.getImages();
        this.actualImage = this.images[0].image;
        setInterval(() => {
          this.changeBackgroundCounter++;
          if (this.changeBackgroundCounter > this.images.length - 1) {
            this.changeBackgroundCounter = 0;
          }
          this.actualImage = this.images[this.changeBackgroundCounter].image;
        }, 5000);
      }
    }
    

    我尽可能多地保留了您的初始代码。我的新变量名为 actualImage,我在 ngOnInit 中设置了一个默认值,就在您从服务中获取所有图像之后。 然后我调用 setInterval 并每 5 秒为 actualImage 设置一个新值!

    https://stackblitz.com/edit/angular-setinterval-f5bghq

    小心:使用 setInterval 时,请在 ngOnDestroy() 上清除它,它可能会导致一些你不想参与的奇怪错误。 只需创建一个其他变量,键入 any,然后执行以下操作:

    this.interval = setInterval(() => {...})
    
    ngOnDestroy() {
      clearInterval(this.interval);
    }
    

    【讨论】:

    • 感谢您帮助我解决我的问题,这真的很有帮助! ?
    • 使用remainder operator this.changeBackgroundCounter=(this.changeBackgroundCounter+1)%this.images.length - 这样可以避免这种“丑陋的if”;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多