Go to page when idle script


(Federico P) #1

Hello !
I’ve been using this script to have the website go the homepage when the user is idle x seconds, it works perfectly using the “hypeDocument.showSceneNamed(‘Home’, hypeDocument.kSceneTransitionCrossfade, 1.1);” command to redirect to the home page, however for some reason once it has transitioned to the homepage it wont stop reloading, and I can’t figure out what’s causing that…

var IDLE_TIMEOUT = 10; //seconds
var _idleSecondsCounter = 0;
document.onclick = function() {
_idleSecondsCounter = 0;
};
document.onmousemove = function() {
_idleSecondsCounter = 0;
};
document.onkeypress = function() {
_idleSecondsCounter = 0;
};
window.setInterval(CheckIdleTime, 1000);

function CheckIdleTime() {
_idleSecondsCounter++;
var oPanel = document.getElementById(“SecondsUntilExpire”);
if (oPanel)
oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + “”;
if (_idleSecondsCounter >= IDLE_TIMEOUT) {
hypeDocument.showSceneNamed(‘Home’, hypeDocument.kSceneTransitionCrossfade, 1.1);
}


#2

Looking at your logic in your code everything looks ok … except a few things :slight_smile:

Your logic is … (I’m assuming this script is on scene load?)

When a mouse move, click or keypress happens then you reset the seconds counter to 0. Good enough.

At 1 sec intervals you increase the seconds counter by 1 and when it reaches the “IDLE_TIMEOUT” you go to the Home scene. Good o!

However, in your code above you haven’t reset the counter when you return “Home” so the counter is counting from it’s last value (10) so therefore the if condition will always be true (>= IDLE_TIMEOUT) hence the reloading. (I am assuming this is a typo in your code to ;))

I would just add a _idleSecondsCounter = 0; after the showSceneNamed call.


(Federico P) #3

Thanks a lot, DBear, that did the trick !
Cheers !


(Jason White) #4

Can you explain the purpose of these lines of code? I’m trying to achieve something similar but can’t seem to make it work. I’m wondering if these are necessary. I removed them in my script.

My function looks like this:

function timer(hypeDocument, element, event) {
    var IDLE_TIMEOUT = 5; //seconds
var _idleSecondsCounter = 0;

document.onclick = function(){
	_idleSecondsCounter = 0;
	};
document.onmousemove = function(){
	_idleSecondsCounter = 0;
	};
document.onkeydown = function(){
	_idleSecondsCounter = 0;
	};
window.setInterval(CheckIdleTime, 1000);

function CheckIdleTime(){
	_idleSecondsCounter++;
	if (_idleSecondsCounter >= IDLE_TIMEOUT) {
		_idleSecondsCounter = 0;
		hypeDocument.showSceneNamed('Title', hypeDocument.kSceneTransitionCrossfade, 1.1);
		}
	}
 }

Even after adding the line to reset counter at the end of the if statement, I still have issues with the ‘Title’ scene resetting. I have added the ‘timer’ function on load of all of the scenes except the ‘Title’ scene. It appears that every time the CheckIdleTime function executes the code from the if statement, it creates a new instance of the timer.

Any suggestions appreciated.


#5

HI @jason

Without seeing your document I can’t tell exactly what you’re doing but if it’s quite similar to the original solution then make sure your _idleSecondsCounter = 0 code is after the call to the “title” scene. Or post your document and we can have a quick look at it and see what’s going on


(Hans-Gerd Claßen) #6

this may also work


(Jason White) #7

@h_classen, thank you. The example you sent works fine. I can definitely use that, but the code is a bit advanced for my basic Javascript skills, and I really like to understand everything that is happening in my designs.

@DBear, I’ve attached an example of what is occurring when I run this function on scene load on all scenes except the title page. It seem to run multiple instances and never really resets. I’m sure that I’m missing some basic understanding of how this works. Thanks again

.timer_test.zip (22.3 KB)