简体   繁体   English

JS监听db / cookies / localhost文件中的更改并立即做出反应

[英]JS listen for a change in db / cookies / localhost files and immediately react

I want to logout a user edited his cookies so their new values are not match to his user info collected in database, like login token. 我想注销一个用户编辑了他的cookie,以便其新值与他在数据库中收集的用户信息(例如登录令牌)不匹配。

As same as this, I want to call http post function to server to fetch a new data as soon as new data was inserted. 与此相同,我想在服务器上调用http post函数以在插入新数据后立即获取新数据。

I have tried setTimeout but after 20 minutes (approximately) the app crashed as it timed out or increased the resources requirements from a given device. 我尝试了setTimeout,但是20分钟(大约)后,该应用因超时或崩溃而增加了崩溃,或者增加了给定设备的资源需求。 I have to mention that I need to modify the changes in DOM as quick as possible, think like of a chat, we do not want to let the user wait for the fresh new message. 我不得不提到,我需要尽快修改DOM中的更改,就像聊天一样,我们不想让用户等待新消息。

How Facebook for example, handles such a thing without getting timed out? 例如,Facebook如何在不超时的情况下处理此类问题? Please try to supply a solution based on JS (pure). 尝试提供基于JS的解决方案(纯)。

Thanks. 谢谢。

I cannot provide a pure JS solution to you, but I believe you are addressing this in a wrong way. 我无法为您提供纯粹的JS解决方案,但我相信您以错误的方式解决了这个问题。

Facebook uses react for their front-end, so they can use component lifecycle to manage updates without reloading the whole page. Facebook使用前端进行响应,因此他们可以使用组件生命周期来管理更新,而无需重新加载整个页面。 I would recommend you using push services or maybe websockets to communicate with your backend in a "realtime" fashion. 我建议您使用推送服务或Websocket以“实时”方式与后端通信。 With web sockets you can inform your frontend about when a change has been made and work from there. 使用Web套接字,您可以通知前端何时进行更改并可以从那里进行工作。 If you are not familiar with them https://socket.io would be a nice place to start. 如果您不熟悉它们, https://socket.io将是一个不错的起点。

This should set you in the right direction. 这应该为您设定正确的方向。

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

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