通过远程 JavaScript 循环创建多个天气馈送器

发布于 2024-09-11 23:41:54 字数 637 浏览 4 评论 0原文

我正在使用天气源在页面上显示天气。我想以 5 秒的间隔循环浏览 2 个或更多天气位置。

我想我可以采用这样的东西如何循环浏览页面?但是没有任何运气。

3 个提要的源代码如下,我想按一定时间间隔不断循环:

<script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=3000"></script>

<script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=3690"></script>

<script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=2000"></script>

提前致谢。

I'm using a weather feed to show weather on a page. I want to cycle through 2 or more weather locations at, say, 5 second intervals.

I thought I could adapt something like this How can I cycle through pages? but haven't had any luck.

The source code is as follows for 3 feeds that I would like to continually cycle through at interval:

<script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=3000"></script>

<script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=3690"></script>

<script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=2000"></script>

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国际总奸 2024-09-18 23:41:54

由于这些小部件使用 document.write(),因此我最初会将所有三个小部件输出到页面,可能是无序列表,然后循环显示它们和隐藏它们。

因此,使用如下标记:

<ul id="cycle">
    <li><script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=3000"></script></li>
    <li><script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=3690"></script></li>
    <li><script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=2000"></script></li>
</ul>

您可以使用 jQuery 执行以下操作:

jQuery(function($) {

    var items = $('#cycle li'), // elements to be cycled through
        interval = 2000,        // time between cycles
        i = 0;

    // hides all the elements, except the first one
    items.not(":first").each(function () {$(this).hide()})

    // show, hide elements every "interval" milliseconds
    window.setInterval(function() {
        $(items[ (i==0) ? (items.length - i) - 1 : i - 1 ]).hide()
        $(items[i++]).show();
        if (!items[i]) i = 0;
    }, interval);

});

Since those widgets use document.write(), i would just output all three to the page initially, in an unordered list maybe, and just cycle through showing and hiding them.

So, using markup like this:

<ul id="cycle">
    <li><script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=3000"></script></li>
    <li><script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=3690"></script></li>
    <li><script type="text/javascript" src="http://www.weatherzone.com.au/woys/graphic_current.jsp?postcode=2000"></script></li>
</ul>

You could use jQuery to do something like this:

jQuery(function($) {

    var items = $('#cycle li'), // elements to be cycled through
        interval = 2000,        // time between cycles
        i = 0;

    // hides all the elements, except the first one
    items.not(":first").each(function () {$(this).hide()})

    // show, hide elements every "interval" milliseconds
    window.setInterval(function() {
        $(items[ (i==0) ? (items.length - i) - 1 : i - 1 ]).hide()
        $(items[i++]).show();
        if (!items[i]) i = 0;
    }, interval);

});
梦太阳 2024-09-18 23:41:54

干得好,克里斯。工作绝对是一种享受。

Nice work Chris. Worked an absolute treat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文