简体   繁体   English

如何在Javascript中创建一个可供所有用户访问的变量?

[英]How can I make a variable in Javascript that is accessible to all users?

I am working on a continuously updated counter that shows how many seconds have passed, in total, during all users' visits to my website. 我正在开发一个不断更新的计数器,该计数器显示在所有用户访问我的网站期间总共经过了几秒钟。 If possible, use Javascript, but otherwise, let me know if a different language is required. 如果可能,请使用Javascript,否则,请告诉我是否需要其他语言。 Sorry, I do not have any code written yet to show, and this might be a stupid question, but I have searched everywhere and have not found a solution. 抱歉,我尚未编写任何代码来显示,这可能是一个愚蠢的问题,但是我到处搜索并且没有找到解决方案。

You can use something like websocket to show the seconds in realtime. 您可以使用诸如websocket之类的工具实时显示秒数。 Use a counter in the server and fire a ticker in every second. 使用服务器中的计数器,并每秒触发一个自动收录器。 When the user visits your site, connect him with the socket and push the updates(number of seconds). 当用户访问您的站点时,将其与套接字连接并推送更新(秒数)。

In this way, every user will get the time in realtime even he revisits your site. 这样,每个用户即使重新访问您的网站也将实时获取时间。

If you are thinking for current session only and user specific, then you can do it easily using JavaScript. 如果您仅考虑当前会话且特定于用户,则可以使用JavaScript轻松完成。 Search the web and there are lots of counter there. 在网上搜索,那里有很多柜台。

You could use the system clock and a known start time. 您可以使用系统时钟和已知的开始时间。 Have javascript grab the system clock's time then subtract the known start time to get the current elapsed time. 让javascript抓取系统时钟的时间,然后减去已知的开始时间,以获取当前经过的时间。 Since the calculation is done when the page is rendered it'll always be accurate. 由于计算是在呈现页面时完成的,因此它将始终是准确的。 And of course have a javascript timer continue ticking off from the calculated time after page render clock style. 当然,还有一个Javascript计时器会在页面渲染时钟样式之后从计算出的时间继续计时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM