角度设置超时不工作Javascript

角度设置超时不工作Javascript,javascript,angular,typescript,settimeout,Javascript,Angular,Typescript,Settimeout,我正在从ngOnInit调用一个函数 public ngOnInit() { this.idleLogout(); } public idleLogout() { let t; window.onload = resetTimer; window.addEventListener('scroll', resetTimer, true); function resetTimer() { clearTimeout(t);

我正在从ngOnInit调用一个函数

  public ngOnInit() {
    this.idleLogout();
  }
  public idleLogout() {
    let t;
    window.onload = resetTimer;
    window.addEventListener('scroll', resetTimer, true);

    function resetTimer() {
      clearTimeout(t);
      t = setTimeout(() => {
        // this.openLearnMoreModal();
      }, 4000);
    }
  }
  public openLearnMoreModal() {
    console.log('here')
  }
我无法从set Timeout函数内部调用OpenLearnMemodal函数,它给出了一个错误


错误类型错误:_this.openlearnomodel不是一个函数

问题在超时内。如果更改“this”引用,请尝试以下示例:

public ngOnInit() {
  this.idleLogout();
}
 public idleLogout() {
  let t;
  const parentThis = this;

  window.onload = resetTimer;
  window.addEventListener('scroll', resetTimer, true); 

 function resetTimer() {

  clearTimeout(t);
    t = setTimeout(() => {
    parentThis.openLearnMoreModal();
  }, 4000); 
 }

问题是在超时内。如果更改“this”引用,请尝试以下示例:

public ngOnInit() {
  this.idleLogout();
}
 public idleLogout() {
  let t;
  const parentThis = this;

  window.onload = resetTimer;
  window.addEventListener('scroll', resetTimer, true); 

 function resetTimer() {

  clearTimeout(t);
    t = setTimeout(() => {
    parentThis.openLearnMoreModal();
  }, 4000); 
 }
或者

使用
绑定(此)

window.onload = resetTimer.bind(this);
window.addEventListener('scroll', resetTimer.bind(this), true); 
public idleLogout() {
  let t;
  const resetTimer = _resetTimer.bind(this);
  window.onload = resetTimer;
  window.addEventListener('scroll', resetTimer, true); 

 function _resetTimer() {

  clearTimeout(t);
    t = setTimeout(() => {
    this.openLearnMoreModal();
  }, 4000); 
 }
使用
箭头功能

public idleLogout() {
  let t;
  const resetTimer = ()=> {
      clearTimeout(t);
      t = setTimeout(() => {
      // this.openLearnMoreModal();
      }, 4000); 
    }
  window.onload = resetTimer;
  window.addEventListener('scroll', resetTimer, true); 
使用此将其绑定到另一个变量:

window.onload = resetTimer.bind(this);
window.addEventListener('scroll', resetTimer.bind(this), true); 
public idleLogout() {
  let t;
  const resetTimer = _resetTimer.bind(this);
  window.onload = resetTimer;
  window.addEventListener('scroll', resetTimer, true); 

 function _resetTimer() {

  clearTimeout(t);
    t = setTimeout(() => {
    this.openLearnMoreModal();
  }, 4000); 
 }
或者

使用
绑定(此)

window.onload = resetTimer.bind(this);
window.addEventListener('scroll', resetTimer.bind(this), true); 
public idleLogout() {
  let t;
  const resetTimer = _resetTimer.bind(this);
  window.onload = resetTimer;
  window.addEventListener('scroll', resetTimer, true); 

 function _resetTimer() {

  clearTimeout(t);
    t = setTimeout(() => {
    this.openLearnMoreModal();
  }, 4000); 
 }
使用
箭头功能

public idleLogout() {
  let t;
  const resetTimer = ()=> {
      clearTimeout(t);
      t = setTimeout(() => {
      // this.openLearnMoreModal();
      }, 4000); 
    }
  window.onload = resetTimer;
  window.addEventListener('scroll', resetTimer, true); 
使用此将其绑定到另一个变量:

window.onload = resetTimer.bind(this);
window.addEventListener('scroll', resetTimer.bind(this), true); 
public idleLogout() {
  let t;
  const resetTimer = _resetTimer.bind(this);
  window.onload = resetTimer;
  window.addEventListener('scroll', resetTimer, true); 

 function _resetTimer() {

  clearTimeout(t);
    t = setTimeout(() => {
    this.openLearnMoreModal();
  }, 4000); 
 }

这应该是正确的答案。另一个答案是误导性的,原因不是
setTimeout
,而是
function
关键字。这应该是正确的答案。另一个答案具有误导性,因为原因不是
setTimeout
,而是
函数
关键字。