On Idle logout?

Dear All, @brainstorrrm @givemeawhisky @brett-haralson

I am working on an on idle logout, I have written the below code and it works to the point that if no actions are taken it starts the process of counting down and calling log out, however I have not been able to achieve the countdown reset piece and this is key, so I can hide and show elements etc but it would be great to achieve the reset piece.

I provide the current code that I have, I am keen to achieve this so if individuals walk away from their computers but leave the screen open and not secure they are then logged out automatically, obviously I know that if someone else is in the room then there is the ability for that individual to access the screen etc but it does aid with providing some security.

If you are able to help with the code and help develop a working example it would be greatly appreciated.

Best wishes,

Si

$w.onReady(function () {
    myFunction();
});

setInterval(function () {
    startCheck();
}, 30000)

function myFunction() {
    $w("#page1").onMouseIn((event) => {
        $w("#text45").hide();
 var X = event.clientX;
        console.log(X)
        memory.setItem("positionX", X);
 var Y = event.clientY;
        console.log(Y)
        memory.setItem("positionY", Y);
        setInterval(function () {
            startCheck();
        }, 30000)
    })
    $w("#page1").onMouseOut((event) => {
        $w("#text45").hide();
 var X = event.clientX;
        console.log(X)
        memory.setItem("positionX", X);
 var Y = event.clientY;
        console.log(Y)
        memory.setItem("positionY", Y);
        setInterval(function () {
            startCheck();
        }, 30000)
    })
    $w("#page1").onClick((event) => {
        $w("#text45").hide();
 var X = event.clientX;
        console.log(X)
        memory.setItem("positionX", X);
 var Y = event.clientY;
        console.log(Y)
        memory.setItem("positionY", Y);
        setInterval(function () {
            startCheck();
        }, 30000)
    })
    $w("#page1").onDblClick((event) => {
        $w("#text45").hide();
 var X = event.clientX;
        console.log(X)
        memory.setItem("positionX", X);
 var Y = event.clientY;
        console.log(Y)
        memory.setItem("positionY", Y);
        setInterval(function () {
            startCheck();
        }, 30000)
    })
}

function startCheck() {
    checkMemory();
}

function checkMemory() {
 var A = memory.getItem("positionX");
    console.log(A)
 var B = memory.getItem("positionY");
    console.log(B)

 let data = [A, B]

 return data;
}

setInterval(function () {
    checkMemory();
}, 30000)

function checkMemoryData() {
 var data = checkMemory();
 var firstA = data[0];
 var secondA = data[1];
 var A = memory.getItem("positionX");
 var B = memory.getItem("positionY");

 if (firstA === A) {
        $w("#text45").show();
        $w('#text45').text = "00:00";
 let isVisible = $w("#text45").isVisible;
 let hidden = $w("#text45").hidden;
 if (isVisible === true) {
            timerForLogout();
        } else if (hidden)
            myFunction();
        }
    }

    setInterval(function () {
        checkMemoryData();
    }, 60000)

 function timerForLogout() {
 var counter = 30;
 var countDown = setInterval(function () {
 let minutes = (counter / 60) | 0;
 let seconds = (counter % 60) | 0;
            minutes = minutes < 10 ? "0" + minutes : minutes;
            seconds = seconds < 10 ? "0" + seconds : seconds;
            $w("#text45").show();
            $w('#text45').text = minutes + ":" + seconds;
            counter--
 if (counter === 0) {
                console.log("User logged Out")
                clearInterval(countDown);
            }
        }, 1000);
    }

Si, there are many ways to implement this.
Using your approach, there are two timers.

  1. Timer on page load
  2. Timer for logout

Your #2 timer looks ok - timerForLogout().
Your #1 timer looks unnecessarily complex and you never reset it - you keep starting new timers.

Place a simple timer in the onReady function that starts when the page loads.
Anytime the user shows signs of being active [ $w(“#page1”).onMouseIn((event) ] etc., you need to

  • clear this active page timer (#1) with clearInterval() and

  • start a new instance of that same timer.

Cheers @brainstorrrm greatly appreciated, I have been going around in my mind for hours trying to figure this one, no chance you could provide an example of where in the code I need to place the timers and the reset? Si

Is this the sort of direction that I should be going?



$w.onReady(function () {
var myTimer = setInterval(myFunction, 30000);
    myFunction();
});

setInterval(function () {
    startCheck();
}, 30000)

function myFunction() {
    $w("#page1").onMouseIn((event) => {
        $w("#text45").hide();
 var X = event.clientX;
        console.log(X)
        memory.setItem("positionX", X);
 var Y = event.clientY;
        console.log(Y)
        memory.setItem("positionY", Y);
        clearInterval(myTimer); 
        myTimer = setInterval(myFunction, 30000);
        setInterval(function () {
            startCheck();
        }, 30000)
    })
    $w("#page1").onMouseOut((event) => {
        $w("#text45").hide();
 var X = event.clientX;
        console.log(X)
        memory.setItem("positionX", X);
 var Y = event.clientY;
        console.log(Y)
        memory.setItem("positionY", Y);
        clearInterval(myTimer);          
        myTimer = setInterval(myFunction, 30000);
        setInterval(function () {
            startCheck();
        }, 30000)
    })
    $w("#page1").onClick((event) => {
        $w("#text45").hide();
 var X = event.clientX;
        console.log(X)
        memory.setItem("positionX", X);
 var Y = event.clientY;
        console.log(Y)
        memory.setItem("positionY", Y);
        clearInterval(myTimer);          
        myTimer = setInterval(myFunction, 30000);
        setInterval(function () {
            startCheck();
        }, 30000)
    })
    $w("#page1").onDblClick((event) => {
        $w("#text45").hide();
 var X = event.clientX;
        console.log(X)
        memory.setItem("positionX", X);
 var Y = event.clientY;
        console.log(Y)
        memory.setItem("positionY", Y);
        clearInterval(myTimer);          
        myTimer = setInterval(myFunction, 30000);
        setInterval(function () {
            startCheck();
        }, 30000)
    })
}

function startCheck() {
    checkMemory();
}

function checkMemory() {
 var A = memory.getItem("positionX");
    console.log(A)
 var B = memory.getItem("positionY");
    console.log(B)

 let data = [A, B]

 return data;
}

setInterval(function () {
    checkMemory();
}, 30000)

function checkMemoryData() {
 var data = checkMemory();
 var firstA = data[0];
 var secondA = data[1];
 var A = memory.getItem("positionX");
 var B = memory.getItem("positionY");

 if (firstA === A) {
        $w("#text45").show();
        $w('#text45').text = "00:00";
 let isVisible = $w("#text45").isVisible;
 let hidden = $w("#text45").hidden;
 if (isVisible === true) {
            timerForLogout();
        } else if (hidden)
            myFunction();
        }
    }

    setInterval(function () {
        checkMemoryData();
    }, 60000)

 function timerForLogout() {
 var counter = 30;
 var countDown = setInterval(function () {
 let minutes = (counter / 60) | 0;
 let seconds = (counter % 60) | 0;
            minutes = minutes < 10 ? "0" + minutes : minutes;
            seconds = seconds < 10 ? "0" + seconds : seconds;
            $w("#text45").show();
            $w('#text45').text = minutes + ":" + seconds;
            counter--
 if (counter === 0) {
                console.log("User logged Out")
                clearInterval(countDown);
            }
        }, 1000);
    }