如何才能定期更新屏幕为雅虎天气/简单派


How can the screen be updated periodically for yahoo weather/simple pie?

我正在使用雅虎天气feed和simplepie为我的数字显示获取天气。因为这是一个在屏幕上运行的网站,我不能一直手动刷新浏览器。

我只是想知道是否有什么例如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(); ?>&deg; &nbsp; <?php echo $forecast->get_high(); ?>&deg; </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);
?>

非常感谢!

S:)

由于您还没有使用JavaScript,因此定期刷新页面的一种非常简单的方法可能是使用元刷新标记。尽管已弃用,但它可能是获取动态页面的一种快速方法。只需将标签添加到页面的<head>:

<meta http-equiv="refresh" content="5"> <!--Refresh the page every 5 seconds-->

但是,您可能需要考虑使用ajax将内容加载到页面中。这可能不是最好的方法,但这里有一些快速和肮脏的代码。

在主页面中,您可以使用jQuery load定期点击PHP脚本的URL,并将内容加载到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(); ?>&deg; &nbsp; <?php echo $forecast->get_high(); ?>&deg; </li>
                <?php endforeach; ?>
            </ul>
            </div><!--degrees-->
    </div> <!--forecast-->
<!--</div> <!--second-->