如何从多个标签页使用相同的javascript计时器? [英] How to use same javascript timer from multiple tab pages?

查看:57
本文介绍了如何从多个标签页使用相同的javascript计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



有没有办法在多个标签页中使用相同的javascript计时器变量?

我可以将它保存到像cookie这样的地方?



更多细节:

我正在开发一个ASP.Net网页,显示1分钟前的倒数计时器会话已过期。我通过添加javascript计时器(setinterval方法)实现了这一点。计时器在每个客户端(Ajax)或服务器回发时重置。

在每个计时器滴答中,我将TimeOutValue-1写入cookie文件。当cookie的值小于或等于60(1分钟)时,我会显示一个包含Extend或Logout按钮的新表单。

问题是如果我打开两个标签页,两者都是页面使用自己的计时器运行并从cookie中减少值。

所以,如果我只有一个页面,那么值会减少,如10,9,8,7,6等......

如果我有两页,它一直在减少10,8,6,4等...... :(



我的方法我的错误因为我对Javascript很新。

请给出一些解决方案或指导我实现这个目标。





感谢先进的

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

推荐答案

1.为什么计时器将值存储在cookie文件中?如果您的页面使用F5重新加载 - 而不是从服务器会话变量中获取计时器值,



如果你的页面没有重新加载,你可以为所有选项卡使用的定时器逻辑创建一些javascript单例(例如组件可能会引发自定义事件''超时'',以及你的页面逻辑将对它做出反应。
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.


我将计时器值存储在cookie文件中,因为我希望每个标签在其中一个标签执行回发时显示相同的计时器值。比方说,用户打开一个标签( tab1)和它从120,119,118等开始倒计时。如果他打开另一个标签(tab2),来自tab1的定时器值没有反映或重置并继续到117,116,...而tab2从120开始倒计时,119等等......

如果在cookie中保存值,如果tab2进行了任何回发,则将cookie值更改回120.然后,tab1也具有相同的值120.并倒计时。 ..



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...

引用:

如果您的页面没有重新加载,您可以创建所有选项卡都使用一些用于计时器逻辑的javascript单例(例如,组件可能会引发自定义事件''超时'',并且您的页面逻辑将对其做出反应。

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.


这篇关于如何从多个标签页使用相同的javascript计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆