简体   繁体   English

如何实现跨页计时器?

[英]How to realize cross page timer?

I am trying to realize booking system on my site. 我正在尝试在我的网站上实现预订系统。 I want to allow user to book terminal entity for 10 minutes. 我想允许用户预订终端实体10分钟。 Entirely, flow is enough big and goes over several pages. 整个流足够大,遍历了几页。
While user navigate on this flow he should see how many time he has to complete operation. 当用户浏览此流程时,他应该看到他必须完成多少次操作。

Please advice me how to realize cross page timer. 请告诉我如何实现跨页面计时器。

I think the most straightforward way would be to create a cookie or an item in the user's localStorage which contains the start time of the transaction. 我认为最直接的方法是在用户的localStorage中创建一个cookie或一个包含事务开始时间的项目。 Upon loading the page, you check for the existence of this value and compare it to the current time, if it exists. 加载页面后,您检查该值是否存在并将其与当前时间(如果存在)进行比较。

You can use setTimeout or setInterval to run timers in the browser. 您可以使用setTimeoutsetInterval在浏览器中运行计时器。 However, a browser window is not guaranteed to continue to execute when it is not active. 但是,不能保证浏览器窗口处于非活动状态时继续执行。 If the user hides the browser or goes to another application, the application cannot be sure that the timer is still running. 如果用户隐藏浏览器或转到其他应用程序,则该应用程序无法确保计时器仍在运行。

There is a newer specification for threading called web workers, which may help to alleviate the setTimeout problem. 有一个新的线程规范,称为Web Worker,可以帮助缓解setTimeout问题。

https://developer.mozilla.org/en-US/docs/Web/API/Worker https://developer.mozilla.org/en-US/docs/Web/API/Worker

As an alternative, I would suggest to store the start time on the server and continuously check the current time against it with polling HTTP requests. 作为替代方案,我建议将开始时间存储在服务器上,并通过轮询HTTP请求将其与当前时间进行连续检查。 Or maybe just check it before they submit. 或只是在提交之前检查一下。

Or as stated by @sholanozie , store it in a browser cookie and just keep checking that. 或如@sholanozie所述 ,将其存储在浏览器cookie中并继续进行检查。

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

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