Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何避免;内部错误:递归太多“;在角度可观测的情况下?_Javascript_Angular_Typescript_Observable - Fatal编程技术网

Javascript 如何避免;内部错误:递归太多“;在角度可观测的情况下?

Javascript 如何避免;内部错误:递归太多“;在角度可观测的情况下?,javascript,angular,typescript,observable,Javascript,Angular,Typescript,Observable,我有一个简单的角度观测,应该连续运行,检查每个循环的时间差,每秒循环一次。当我运行它时,我得到了“InternalError:太多递归”。据此: 我用的是正确的方法。如何修复 可观察的角度: export class MyService { private lastHeartBeatTime = null; // Time of the last heartbeat private heartBeatTimeoutId = null; private secondsToHeartb

我有一个简单的角度观测,应该连续运行,检查每个循环的时间差,每秒循环一次。当我运行它时,我得到了“InternalError:太多递归”。据此: 我用的是正确的方法。如何修复

可观察的角度:

export class MyService {
  private lastHeartBeatTime = null; // Time of the last heartbeat
  private heartBeatTimeoutId = null;

  private secondsToHeartbeatAlert = 5; // Number of seconds of no heartbeat 

  constructor() {
    this.lastHeartBeatTime = performance.now(); // Initialise local heartbeat variable with current time
  }

  subscribeToHeartbeat(callbackfn): any {
    // Post a value via the observable to cause an alert to be raised:
    if((performance.now() - this.lastHeartBeatTime) / 1000 >= this.secondsToHeartbeatAlert) return(true);
    // Create a new timeout to call this function again after the specified num of milliseconds (e.g. after 1 second):

    // **** PROBLEM HERE: ***
    else this.heartBeatTimeoutId = setTimeout(this.subscribeToHeartbeat(callbackfn), 1000);
  }
}
其他组件内的订阅:

// Subscribe to heartbeat over websockets:
MyService.subscribeToHeartbeat(this.handleHeartbeatFailure);

// Handler:
  handleHeartbeatFailure:any = (message) => {
   alert('Websocket is down!")
  }

您正在调用函数,但没有在超时时分配它

setTimeout(this.subscribeToHeartbeat(callbackfn), 1000);
需要

setTimeout(() => this.subscribeToHeartbeat(callbackfn), 1000);
或者你可以使用bind

setTimeout(this.subscribeToHeartbeat.bind(this, callbackfn), 1000);

您正在调用函数,但没有在超时时分配它

setTimeout(this.subscribeToHeartbeat(callbackfn), 1000);
需要

setTimeout(() => this.subscribeToHeartbeat(callbackfn), 1000);
或者你可以使用bind

setTimeout(this.subscribeToHeartbeat.bind(this, callbackfn), 1000);
setTimeout(this.subscribeToHeartbeat(callbackfn),1000)
错误,您正在调用函数,而没有分配它…
setTimeout(this.subscribeToHeartbeat(callbackfn),1000)错误,您正在调用函数,而不是分配它。。。