创建基于 Javascript 的新闻行情

发布于 2024-10-14 13:45:39 字数 466 浏览 3 评论 0 原文

我正在创建某种股票代码,并且需要它一直运行,直到页面明显关闭或导航离开。

这个脚本将只是循环通过 li (这是我到目前为止所得到的):

$("li").hide(); //hides all "li" onload
var lis = $("ul").children("li").size(); //counts how many "li"s there are
var count = 0;
while(true)
{
    count++;
    if(count>lis) //checks if count needs to be reset
    count = 1;

    $("li:nth-child(" + count + ")").fadeIn('slow');
    setTimeout('', 4000);
}

显然,由于无限循环,页面不会加载。有人对我应该如何解决这个问题有任何建议吗?

I'm creating a ticker of sorts, and need it to be running until the page is obviously closed or navigated away from.

This script will just be cycling through li (this is what I've got so far):

$("li").hide(); //hides all "li" onload
var lis = $("ul").children("li").size(); //counts how many "li"s there are
var count = 0;
while(true)
{
    count++;
    if(count>lis) //checks if count needs to be reset
    count = 1;

    $("li:nth-child(" + count + ")").fadeIn('slow');
    setTimeout('', 4000);
}

Obviously the page won't load because of the the infinite loop. Does anyone have any suggestions about how I should go about this?

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

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

发布评论

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

评论(4

宛菡 2024-10-21 13:45:39
var ticker = $('ul.ticker');
ticker.children(':first').show().siblings().hide();

setInterval(function() {
    ticker.find(':visible').fadeOut(function() {
        $(this).appendTo(ticker);
        ticker.children(':first').show();
    });
},2000);

演示

var ticker = $('ul.ticker');
ticker.children(':first').show().siblings().hide();

setInterval(function() {
    ticker.find(':visible').fadeOut(function() {
        $(this).appendTo(ticker);
        ticker.children(':first').show();
    });
},2000);

demo

波浪屿的海角声 2024-10-21 13:45:39

您应该让 setTimeout 指向您希望每次触发的函数,并让该函数再次调用 setTimeout。它实际上是无限轮询,但具有内置延迟。如果您想中断(即不再调用 setTimeout),它还可以让您设置一个标志。

You should have your setTimeout point to the function that you want to have fire each time, and have that function also call setTimeout again. It's effectively infinite polling, but with the built-in delay. It also lets you set a flag if you want to break the (i.e. by not calling setTimeout again).

南冥有猫 2024-10-21 13:45:39

还有其他类似 jquery 插件的东西 (jQuery webTicker< /a>) 为您完成所有工作。例如,您可以

<ul id='webticker'>
  <li>text</li>
</ul>

在其上使用 jquery

jQuery('#webticker').webTicker();

制作一个未排序的列表,这将自动启动您的旋转脚本,您还需要一些额外的 CSS,这些 CSS 是随插件本身一起提供的,用于可以修改的样式。该脚本包括当鼠标位于 webticker 顶部时自动停止功能,同时您可以具有淡入和淡出效果,如 webticker 示例

There is also something else like a jquery plugin (jQuery webTicker) that does all the work for you. For example you can just do an unsorted list

<ul id='webticker'>
  <li>text</li>
</ul>

use jquery on top of it

jQuery('#webticker').webTicker();

and this will automatically start your rotating script you will also need some additional CSS which is provided along the plugin itself for styling which can be modified. The script includes and automatic stop function when mouse is on top of the webticker whilst you can have fade in and fade out effects as explained in the webticker example on the download website

老娘不死你永远是小三 2024-10-21 13:45:39

为了补充雷格尔的精彩答案,

var ticker = $('ul.ticker');
ticker.children(':first').show().siblings().hide();

setInterval(function() {
ticker.find(':visible').fadeOut(function() {
    $(this).appendTo(ticker);

    // fadeIn() rather than show() here to create a continuously fading effect.

    ticker.children(':first').fadeIn();
});
},2000);

To add to Reigel's brilliant answer,

var ticker = $('ul.ticker');
ticker.children(':first').show().siblings().hide();

setInterval(function() {
ticker.find(':visible').fadeOut(function() {
    $(this).appendTo(ticker);

    // fadeIn() rather than show() here to create a continuously fading effect.

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