June 3, 2010

Hide a div after some time if no activity occurs on that div

Question by snell

I Am trying to hide a div when the user doesn’t click any button on it for 15 seconds.

I know how to hide and display the div by using element.style.display = 'none'; but I am not able to understand how to calculate the time after last activity on the div.

Answer by Starx

If you are familier with jquery, there is the plugin known as jquery timer(the link is below), which is going to enable you run certain code in regular interval of time.

Whenever a page or a div is initiated, setup a session, and keep the time in the session variable like $_SESSION['checkedtime'] = time();

Now use the jquery timer to sent a psot request to a file which checks the value and return the activity status a basic example of that page is like this

check.php

$oldtime = $_SESSION['checkedtime'];
if($oldtime<time()-15) { return 0; }
else { return 1; }

to setup a time function see the demo page

http://www.evanbyrne.com/article/jquery-timer-plugin

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!