保存动态网页界面状态的最佳方法
基本上在我的网站上,我有一个带有一堆盒子的侧边栏;每个框都可以由用户折叠或展开。我需要保存当前登录用户的每个框的状态。 我不想使用cookie,因为如果用户更改浏览器或计算机,所有框都会进入默认状态。
我是否应该将此信息保存在数据库中,以便每次用户折叠/展开框时进行查询?你会怎么处理这个?谢谢。
编辑:如果用户仅仅因为喜欢盒子的动画而重复单击切换按钮(例如两秒内十次)怎么办?他将在两秒钟内提出十个疑问。所以也许问题应该是:什么时候我应该保存这些数据?
Basically in my website I have a sidebar with a stack of boxes; each box can be collapsed or expanded by the user. I need to save the status of each box for the currently logged in user.
I don't want to use cookies because if an user changes browser or computer, all the boxes will go to the default status.
Should I save this information on the database making a query every time the user collapses/expands a box? How would you handle this? Thanks.
Edit: What if an user clicks on the toggle button repeatedly, like ten times in two seconds, just because he enjoys the boxes' animation? He will make ten queries in two seconds. So maybe the question should be: when should I save this data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次框更改时调用(客户端)“更改”函数。
保留两项(客户端)状态:最后一次向服务器发送更新的时间以及是否设置了计时器。
保留
编写一个(客户端)“更新”函数,用于发送更新并更新状态以标记最近一次更新。
当调用改变的函数时:如果设置了定时器,则立即返回;如果从未发送过更新或者最后一次更新是在十多秒前发送的,则调用更新函数并返回。否则设置一个定时器,在十秒后发送更新。
定时器回调应该简单地清除定时器标志并调用更新函数。
还在 unload 事件上检查是否设置了计时器以及是否清除了计时器并调用计时器回调函数。
因此,结果是您立即发送更新除非当用户抖动时,在这种情况下您最多每十秒发送一次更新。
在某些情况下,您可能会丢失更新,但这可能仅在用户进行切换并在计时器触发之前关闭页面时才会发生,在这种情况下,他可能不会注意到。
Call a (client-side) "changed" function every time a box changes.
Keep two items of (client-side) state: the time the last update to the server was sent and whether a timer has been set.
Write a (client-side) "update" function that sends the update and updates the state to mark that the last update was just now.
When the changed function is called: if a timer is set, then return immediately; if an update has never been sent or the last update was sent more than ten seconds ago, then call the update function and return. Otherwise set a timer to send the update after ten seconds.
The timer callback should simply clear the timer flag and call the update function.
Also on an unload event check if a timer was set and if it was then clear the timer and call the timer callback function.
So the result is that you send the update immediately except when the user is flapping, in which case you only send an update every ten seconds at most.
There might be some cases where you lose an update, but this is likely to only happen if the user was playing with the toggling and then closed the page before the timer fired, in which case he probably won't notice anyway.
如果您需要从多台计算机保留这些选项,您将需要某种形式的服务器端存储。
这可以是数据库或平面文件。选择取决于您可用的资源、技能以及扩展的需要。如果您只有几个用户,平面文件可能是您的最佳选择。
If you need to persist these options from multiple computers you will need some form of server side storage.
This could be database or flat file. The choice depends on what you have available, skill set, and the need to scale. If you are going to have just a few users, a flat file may be your best choice.