创建基于 Javascript 的新闻行情
我正在创建某种股票代码,并且需要它一直运行,直到页面明显关闭或导航离开。
这个脚本将只是循环通过 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);
}
显然,由于无限循环,页面不会加载。有人对我应该如何解决这个问题有任何建议吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
演示
demo
您应该让 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).
还有其他类似 jquery 插件的东西 (jQuery webTicker< /a>) 为您完成所有工作。例如,您可以
在其上使用 jquery
制作一个未排序的列表,这将自动启动您的旋转脚本,您还需要一些额外的 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
use jquery on top of it
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
为了补充雷格尔的精彩答案,
To add to Reigel's brilliant answer,