provision to check if webviewer is idle for an hour

18 views
Skip to first unread message

rakesh mishra

unread,
Mar 12, 2021, 6:27:19 PM3/12/21
to PDFTron WebViewer
Hi,

We are into a scenario where we need to check if user is inactive for an hour (i.e. WebViewer window/screen is idle). Is there any event or api available to check that, I couldn't find in the documentation.
If there is any way, please do let me know.

Thanks
Rakesh

Oscar Zhang

unread,
Mar 16, 2021, 9:55:15 PM3/16/21
to PDFTron WebViewer
Hey Rakesh,

Unfortunately, we don't have an API to check user's activities, but I believe this is something you can implement with pure JavaScript, for example:

Webviewer({...},
  document.getElementById('viewer')
).then(async (instance) => {
  function activityWatcher(){

    //The number of seconds that have passed since the user was active.
    let secondsSinceLastActivity = 0;

    //Max Inactivity time, for example: 3 seconds.
    const maxInactivity = 3;

    //An array of DOM events that should be interpreted as user activity.
    const activityEvents = [
      'mousedown', 'mousemove', 'keydown',
      'scroll', 'touchstart'
    ];

    //Setup the setInterval method to run
    //every second. 1000 milliseconds = 1 second.
    setInterval(function(){
        secondsSinceLastActivity++;
        console.log(secondsSinceLastActivity + ' seconds since the user was last active');
        //if the user has been inactive or idle for longer
        //then the seconds specified in maxInactivity
        if(secondsSinceLastActivity > maxInactivity){
            console.log('User has been inactive for more than ' + maxInactivity + ' seconds');
        }
    }, 1000);

    //The function that will be called whenever a user is active
    function activity(){
        //reset the secondsSinceLastActivity variable
        //back to 0
        secondsSinceLastActivity = 0;
    }

    activityEvents.forEach(function(eventName) {
      //add these events to the webviewer iframe
      const viewerWindow = document.getElementById('viewer').querySelector('iframe').contentWindow;
      //register the activity function as the listener parameter.
      viewerWindow.document.body.addEventListener(eventName, activity, true);
    });
  }

  activityWatcher();

});


The code above is from https://thisinterestsme.com/javascript-detect-user-activity/, I modified it a little bit to make it works in WebViewer, but there are also many other posts/tutorials that you may find useful:


Best Regards,
Oscar Zhang
Software Developer
PDFTron Systems, Inc.

CONFIDENTIALITY NOTICE: This message (and any attachment to it) is intended only for the use of the individual or entity to which it is addressed in the header, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. Any reproduction, distribution, modification or use of the contents of this message (and any attachment to it) by any individual or entity other than the intended recipient is prohibited. If you have received this communication in error, please notify us immediately and delete the original.
Reply all
Reply to author
Forward
0 new messages