-2

I have to write a global method in angular 2+(i am using angular 6).The reason behind using global method is If the system is idle for X minutes then I have to show dialog box.I am planning to call global method on every click and store current time in SessionStorage and If different between last accessed time is more than X minutes then show dialog.Where to write function that is accessible from all everywhere?

If you think this approach having flaws,could you please help in pointing out those.

F11
  • 3,703
  • 12
  • 49
  • 83

2 Answers2

0

You can place a function in the window reference and it will be global.

window.functionName = function() {};

But you do not need to do this to solve your problem. Why not just create an Angular service. The service should set a timer for x minutes and if that ever is reached it can take whatever action is necessary. Once that is in place you can implement the various ways to reset the timer back to zero. Probably just need to bind to click events on the document.

Intercept all mouse events

0

You should create a service for this. For instance:

import { Injectable } from '@angular/core';

@Injectable()
export class TimerService {

  private static setTime(time: Date) {
     localStorage.setItem('time', time)
  }
}

Keep in mind, this is extremely basic version. But then, in every component that has a click function, just import the TimerService then add this to every click event:

TimerService.setTime(new Date())
Chris Stanley
  • 2,766
  • 2
  • 13
  • 18