Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

Is there a way to use same javascript timer variable from multiple tab pages?
Can I save it to somewhere like cookie?

For more details:
I'm developing a ASP.Net web page that shows countdown timer 1 minute before session was expired. I implemented this by adding javascript timer (setinterval method). Timer was reset on every Client (Ajax) or Server postbacks.
In every timer tick, I write TimeOutValue-1 into cookie file. When the value of cookie less than or equal to 60 (1 minute), I show a new form that contains "Extend" or "Logout" buttons.
Problem is if I open two tab pages, both page run with their own timers and reducing value from cookie together.
So, if I have only one page, value will be reduce like 10, 9, 8, 7, 6, etc...
If I have two pages, it have been reducing 10, 8, 6, 4, etc... :(

My approach my be wrong coz I am very new to Javascript.
Please give some solution or guide me to achieve this goal.


Thanks in advanced
Posted
Updated 15-Jan-13 20:07pm
v2

1. Why timer stores value in a cookie file? If your page reloads with F5 - than you can get the timer value from server session variables,

if your page is not reloaded, you can create some javascript singleton for timer logic to be used by all tabs (for example component may raise the custom event 'out of time', and your page logic will react on it.
 
Share this answer
 
I stored timer value in cookie file because I want every tabs to show same timer value when one of the tab performs a postback. Let say, user open a tab (tab1) and it's counting down from 120, 119, 118, etc. If he open another tab (tab2), timer value from tab1 was not reflect or reset and continuing to 117, 116, ... while tab2 have counting down from 120, 119, etc...
With saving value in cookie, if tab2 have make any postback, cookie value was changed back to 120. Then, tab1 also have the same value 120. And counting down...

Quote:
if your page is not reloaded, you can create some javascript singleton for timer logic to be used by all tabs (for example component may raise the custom event 'out of time', and your page logic will react on it.

I don't know how to do this. Can you explain more details with sample codes?

Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900