本文介绍了Angular2:setTimeout只调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Angular2中实现了需要使用 setTimeout 的功能。

I'm implementing functionality in Angular2 that requires the use of setTimeout.

我的代码:

  public ngAfterViewInit(): void {
    this.authenticate_loop();
  }

  private authenticate_loop() {
    setTimeout (() => {
      console.log("Hello from setTimeout");
    }, 500)
  }

setTimeout ngAfterViewInit 启动,但循环仅执行一次,例如。 Hello fromsetTimeout只打印一次。

setTimeout is started by ngAfterViewInit but the loop is only executed once, eg. "Hello fromsetTimeout" is only printed once.

问题:如何更改代码以使setTimeout有效?

Question: How can I change the code to make the setTimeout work?

推荐答案

 private authenticate_loop() {
    setInterval (() => {
      console.log("Hello from setInterval");
    }, 500)
 }

setTimeout 将只运行一次,除非您创建另一个 setTimeout

setTimeout will run just one time, unless you create another setTimeout.

这篇关于Angular2:setTimeout只调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:22