如何定期更新yahoo weather/simple pie的屏幕?
我使用雅虎天气馈送和simplepie为我的数字显示获取天气。由于它是一个在屏幕上运行的网站,我无法一直手动刷新浏览器。如何定期更新yahoo weather/simple pie的屏幕?
我只是想知道有什么东西,例如jQuery或PHP文档可以帮助我定期更新天气信息随着天气的变化?如果是的话,你可以给我一些指导如何实施?非常感谢您的帮助。
下面是我的一些代码,以防万一它是有用的
<div id="weather">
<div id="title">
<ul>
<!--<li> Outside Temp </li>-->
<li> Today </li>
<li> Tomorrow </li>
</ul>
</div> <!--title-->
<div id="forecast">
<div id="images">
<ul>
<?php foreach ($weather->get_forecasts() as $forecast): ?>
<li><img src="<?php echo $forecast->get_image(); ?>" alt="Weather" /></li>
<?php endforeach; ?>
</ul>
</div> <!--images-->
<div id="degrees">
<ul>
<?php foreach ($weather->get_forecasts() as $forecast): ?>
<li><?php echo $forecast->get_low(); ?>° <?php echo $forecast->get_high(); ?>° </li>
<?php endforeach; ?>
</ul>
</div><!--degrees-->
</div> <!--forecast-->
<!--</div> <!--second-->
一些头部分信息:
<php
require_once('simplepie/simplepie.inc');
require_once('simplepie/simplepie_yahoo_weather.inc');
$feed = new SimplePie();
$feed->set_feed_url('http://weather.yahooapis.com/forecastrss?w=1234567&u=c');
$feed->set_item_class('SimplePie_Item_YWeather');
$feed->init();
$weather = $feed->get_item(0);
?>
非常感谢您!
小号:)
既然你还没有使用JavaScript,建议您定期刷新页面可能是使用的meta refresh tag一个非常简单的方法。尽管已弃用,但它可能是获取动态页面的快捷方式。只需将标签添加到页面的<head>
:
<meta http-equiv="refresh" content="5"> <!--Refresh the page every 5 seconds-->
然而,你可能要考虑使用Ajax加载内容到页面代替。这可能不是最好的方法,但这里有一些快速而脏的代码。
在主要页面,你可以使用jQuery load
定期打网址为你的PHP脚本和内容加载到div
显示:
<html>
<head>
<script type="text/javascript">
$(document).ready(function(){
setTimeout(loadWeather, 5000); //using setTimeout instead of a setInterval to avoid having multiple queries queue up when the first one fails to return within the timeout period
});
function loadWeather(){
$('#weather').load('weather.php);
setTimeout(loadWeather, 5000);
}
</script>
</head>
<body>
<div id="weather" />
</body>
</html>
其中weather.php
可能是这个样子:
<div id="weather">
<div id="title">
<ul>
<!--<li> Outside Temp </li>-->
<li> Today </li>
<li> Tomorrow </li>
</ul>
</div> <!--title-->
<div id="forecast">
<div id="images">
<ul>
<?php foreach ($weather->get_forecasts() as $forecast): ?>
<li><img src="<?php echo $forecast->get_image(); ?>" alt="Weather" /></li>
<?php endforeach; ?>
</ul>
</div> <!--images-->
<div id="degrees">
<ul>
<?php foreach ($weather->get_forecasts() as $forecast): ?>
<li><?php echo $forecast->get_low(); ?>° <?php echo $forecast->get_high(); ?>° </li>
<?php endforeach; ?>
</ul>
</div><!--degrees-->
</div> <!--forecast-->
<!--</div> <!--second-->
您好,编码,请给我一些例子/代码工作?我已经使用JavaScript显示的其他部分,我尝试了一些jQuery刷新,但它似乎阻止了我的网站的一些工作。任何帮助表示赞赏。谢谢,S :) – grumpypanda
@SamIAm请参阅我的更新答案为一个可能(简单化)的解决方案。 –
嗨编码,非常感谢您的回复,我真的很感激。我只是在写我发现的东西,在这里你比我快。我找到了一个ajax和javascript的方式,这里是代码您是否认为它的工作方式与您的建议相同?哪种方法更好?非常感谢你! S :) – grumpypanda
在一定的时间间隔内使用ajax请求使用setInterval –