admin管理员组

文章数量:1319467

I have to detect user's idle time out in my application. My application is using both native javascript and angular. in my HTML + JavaScript page i implemented below:

<script>
var inactivityTime = function () {
    var time;
    document.onmousemove = resetTimer;
    document.onkeypress = resetTimer;
    document.onclick = resetTimer;
    document.onwheel = resetTimer;

    function callApi() {
        alert("Idle for 3 seconds. You can call your api here");
    }

    function resetTimer() {
        clearTimeout(time);
        time = setTimeout(callApi, 3000); 
        // 5 minute = 60000 * 5;
        // 1000 milliseconds = 1 second
    }
};

window.onload = function() {
  inactivityTime(); 
}
</script>

and it is working as expected.

whereas the same i am using in angular project, angular code is also working but 1 small issue observed please see my angular code first:

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

@Component({
  selector: 'app-root',
  templateUrl: './appponent.html',
  styleUrls: ['./appponent.css']
})
export class AppComponent {
  title = 'project';
  time;

  ngOnInit() {  
    this.inactivityTime();  
    this.resetTimer();
  }

  inactivityTime() {
    document.onmousemove = this.resetTimer;
    document.onkeypress = this.resetTimer;
    document.onclick = this.resetTimer;
    document.onwheel = this.resetTimer;

  }

  resetTimer() {
    clearTimeout(this.time);
    this.time = setTimeout(() => {
      alert('Idle for 3 seconds. You can call your api here');
    }, 3000);
  }
}

the problem i am facing is - on page load even if am doing mouse move / key press, the alert is getting called. after that everything is working as expected. But on page load it is not working.

Requesting help here.

thank you!

I have to detect user's idle time out in my application. My application is using both native javascript and angular. in my HTML + JavaScript page i implemented below:

<script>
var inactivityTime = function () {
    var time;
    document.onmousemove = resetTimer;
    document.onkeypress = resetTimer;
    document.onclick = resetTimer;
    document.onwheel = resetTimer;

    function callApi() {
        alert("Idle for 3 seconds. You can call your api here");
    }

    function resetTimer() {
        clearTimeout(time);
        time = setTimeout(callApi, 3000); 
        // 5 minute = 60000 * 5;
        // 1000 milliseconds = 1 second
    }
};

window.onload = function() {
  inactivityTime(); 
}
</script>

and it is working as expected.

whereas the same i am using in angular project, angular code is also working but 1 small issue observed please see my angular code first:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.ponent.html',
  styleUrls: ['./app.ponent.css']
})
export class AppComponent {
  title = 'project';
  time;

  ngOnInit() {  
    this.inactivityTime();  
    this.resetTimer();
  }

  inactivityTime() {
    document.onmousemove = this.resetTimer;
    document.onkeypress = this.resetTimer;
    document.onclick = this.resetTimer;
    document.onwheel = this.resetTimer;

  }

  resetTimer() {
    clearTimeout(this.time);
    this.time = setTimeout(() => {
      alert('Idle for 3 seconds. You can call your api here');
    }, 3000);
  }
}

the problem i am facing is - on page load even if am doing mouse move / key press, the alert is getting called. after that everything is working as expected. But on page load it is not working.

Requesting help here.

thank you!

Share Improve this question asked Mar 27, 2020 at 7:11 AChauhanAChauhan 2273 silver badges18 bronze badges 1
  • 1 set up you listeners after document.load – enno.void Commented Mar 27, 2020 at 8:05
Add a ment  | 

2 Answers 2

Reset to default 8

The best approach is to use Angular HostListener decorator and bind it to document:

export class AppComponent {
  time: number;

  ngOnInit() {  
    this.resetTimer();
  }

  @HostListener('document:mousemove')
  @HostListener('document:keypress')
  @HostListener('document:click')
  @HostListener('document:wheel')
  resetTimer() {
    clearTimeout(this.time);
    this.time = setTimeout(() => {
      alert('Idle for 3 seconds. You can call your api here');
    }, 3000);
  }
}

Here is solution in stackblitz

The easiest option would be to use angular-user-idle.

https://www.npmjs./package/angular-user-idle

Include this in app.module.ts: imports: [ .. .. UserIdleModule.forRoot({idle: 3, timeout: 0, ping: 0}) ]

app.ponent.ts

ngOnInit() {  
    this.userIdle.startWatching();
    this.userIdle.onTimerStart().subscribe(count =>{
      console.log(count);
      if(count == 3){
        alert('Idle for 3 seconds. You can call your api here');
        this.userIdle.resetTimer();
      }
    });
  }

  @HostListener('document:keyup', [])
  @HostListener('document:click', [])
  @HostListener('document:wheel', [])
  @HostListener('document:mousemove', [])
  restart() :void{
    this.userIdle.resetTimer();
  }

本文标签: javascriptHow to implement Idle time in angular using native JS codeStack Overflow