简体   繁体   中英

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.

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?

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

It is not possible. For PHP to send data from server to client the request has to be made synchronously. 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. If you combine jQuery and Ajax (they are fairly easy to learn JavaScript libraries) you can do pretty cool stuff. What you are looking to do is send an XHR request can achieve asynchronous data manipulation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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