1

sample.component.ts

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-sample',
  templateUrl: './sample.component.html',
  styleUrls: ['./sample.component.scss']
})
export class Sample implements OnInit {
  closeResult: string;
  focuscount:number=0;
  fcount:any;

  constructor(private router: Router) {
    ngOnInit() {
      let self = this;
      function final(){
      setTimeout(() => {
      self.router.navigate(['pattern']);
      }, 120000);
    }
  **Checking screen focus**  
  function onBlur() {
    document.body.className = 'blurred';
    focuscount+=focuscount
    if(this.focuscount>=3){
    console.log("Inside focuscount");
    final();

}
};
}

Not able to access this function. Getting error Cannot find name 'finalclose'

finalclose(){
  alert("closed");

}
}

I'm trying to redirect to pattern page when the condition is met. I'm getting type error. Please help me fix it.

Thanks in advance

Anjana
  • 366
  • 5
  • 21
  • 1
    Try not defining `final`. Call `setTimeout(...)` directly in `onBlur` instead. Alternatively, you could define `final` as a method of the class, not as a `function` in the constructor. Your `setTimeout` callback should work fine as it is. – ConnorsFan Apr 24 '20 at 14:16

1 Answers1

3

You can not use this inside arrow function change to

 ngOnInit() {
    let self = this;
    function final(){
    setTimeout(() => {
      self.router.navigate(['pattern']);
  }, 120000);
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62