简体   繁体   English

如何持续将值从PHP传递到javascript

[英]How do I continuously pass values from PHP to javascript

I need to update my UI (webpage) periodically with new values. 我需要使用新值定期更新UI(网页)。 I have PHP code that grabs the values form a database, but I am not 100% sure on what is the best way of getting that data to the UI side. 我有PHP代码可以从数据库中获取值,但我不确定100%将数据传输到UI端的最佳方法是什么。

The complication here is that: 1. I need to constantly update the values (every second) 2. I need to update many different areas of the page independently 这里的复杂之处在于:1.我需要不断更新值(每秒)2.我需要独立更新页面的许多不同区域

Thanks, 谢谢,

You use javascript to ask a PHP script for the values. 您可以使用javascript向PHP脚本询问值。 You don't push from PHP to javascript. 您不会从PHP推送到javascript。

You'll probably want to use AJAX and setInterval() 您可能需要使用AJAX和setInterval()

http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/ http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

What you probably want is comet , not setInterval as you'll overwhelm your servers with a lot of unnecessary traffic. 您可能想要的是Comet ,而不是setInterval,因为您将因大量不必要的流量而使服务器不堪重负。

This is what Facebook does for their chat features, and their live feeds (or at least used to do, I haven't looked at it in quite some time. 这就是Facebook为他们的聊天功能和实时供稿所做的(或者至少以前是这样做的,我已经有一段时间没有对此进行研究了)。

You should also look into http://dev.w3.org/html5/websockets/ More and more browsers starts to support them and it's a good way to constantly pass information back and forth. 您还应该研究一下http://dev.w3.org/html5/websockets/越来越多的浏览器开始支持它们,这是不断地来回传递信息的好方法。

You can check for browser support and use it if it exist. 您可以检查浏览器支持并使用它(如果存在)。

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

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