简体   繁体   English

PHP是否可以在不刷新页面的情况下更新值?

[英]Is it possible in PHP to update a value without refreshing the page?

I want to get a value from a URL and play a sound once one of its elements crosses a value and update it every minute so that it can be displayed on the page without refreshing it. 我想从URL获取值,并在其元素之一超过值后播放声音,并每分钟更新一次,这样它就可以在页面上显示而无需刷新。

This is what I have so far: 这是我到目前为止的内容:

<?php
//while(true)
//{
  $string = file_get_contents("http://www.bitstamp.net/api/ticker/");
  $content = json_decode($string, true);
  echo $content["timestamp"],"\n", $content["last"];
// sleep(30);
//}
?>

If I uncomment the loop the page doesn't load (I guess it's trying to fetch all the iterations of the loop before displaying anything). 如果取消注释循环,则页面不会加载(我猜它正在尝试在显示任何内容之前获取循环的所有迭代)。 Is there a way I can do this with php so that the value will auto-update itself? 有没有办法可以用php做到这一点,以便该值会​​自动更新?

Any ideas? 有任何想法吗?

不,您可能要尝试使用Javascript来实现此功能,因为它是在客户端完成的。

You should not use a loop, but delay the execution of the next file fetch using the function 您不应使用循环,而应使用函数延迟下一次文件获取的执行

settimeout();

http://www.w3schools.com/jsref/met_win_settimeout.asp http://www.w3schools.com/jsref/met_win_settimeout.asp

It is not possible. 这不可能。 For PHP to send data from server to client the request has to be made synchronously. 为了使PHP从服务器向客户端发送数据,必须同步进行请求。 Most common way of doing this asynchronously is using Ajax and as of recently a good way is to approach front end development with Angularjs. 异步执行此操作的最常见方法是使用Ajax,而最近一种好方法是使用Angularjs进行前端开发。 If you combine jQuery and Ajax (they are fairly easy to learn JavaScript libraries) you can do pretty cool stuff. 如果结合使用jQuery和Ajax(它们非常容易学习JavaScript库),那么您可以做的很酷。 What you are looking to do is send an XHR request can achieve asynchronous data manipulation. 您要做的是发送XHR请求可以实现异步数据操作。

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

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