即使 setTimeout 设置为 10 秒,jQuery 横幅旋转速度也太快
我为我创建的网站创建了一个简单的旋转横幅 http://sandbox.worldwatchlist.us。
一切都工作正常,直到我转到浏览器中的另一个选项卡,然后几秒钟后回来......横幅每 0.5 秒或更短时间旋转一次。
我不知道为什么会发生这种情况,特别是因为我在 javascript 中使用了 setTimeout 并将其设置为 10 秒(10000 毫秒)。
以下是服务器上的banner.js 文件中的所有代码。请指出任何可以解决这个问题的东西。您可以查看上面的网站,也可以查看我在 http:// /www.youtube.com/watch?v=HWb2uc6KpFM。我测试过每个浏览器都有这个问题。
我到处寻找“jquery 横幅旋转太快”的答案,除了“使用 setTimeout”之外没有其他答案可以解决我的问题。
var banner_width = 960;
var default_pos = 0;
var current_pos = 0;
var next_pos;
var speed = 500;
var banner_tot; //number of banners
var current_num = 1; //current banner number
$(function(){
banner_tot = $(".banner_item").length;
$(".banner_slider").css({width:(banner_tot*banner_width)});
setTimeout( "slide()", 10000 );
});
function slide(){
if(current_num==(banner_tot)){
next_pos= 0;
$(".banner_slider").animate({left: next_pos}, speed);
current_num=1;
setTimeout( "slide()", 10000 );
}
else{
next_pos= current_num*(-banner_width);
$(".banner_slider").animate({left: next_pos}, speed);
current_num++;
setTimeout( "slide()", 10000 );
}
}
I created a simple rotating banner for a website I created http://sandbox.worldwatchlist.us.
Everything was working fine until I went to another tab in my browser and then came back a few seconds later...the banner was rotating every .5 seconds or less.
I have no clue why this is happening especially since I used setTimeout
in the javascript and set it to 10 seconds (10000 ms).
Below is all the code in the banner.js file on the server. Please point out anything that will solve this problem. You can take a look at the website above, or also look at the video screenshot I took at http://www.youtube.com/watch?v=HWb2uc6KpFM. Every browser has this problem that I've tested.
I've looked all over the place for an answer to "jquery banner rotating too quickly" and no answer to solve my problem other than "use setTimeout".
var banner_width = 960;
var default_pos = 0;
var current_pos = 0;
var next_pos;
var speed = 500;
var banner_tot; //number of banners
var current_num = 1; //current banner number
$(function(){
banner_tot = $(".banner_item").length;
$(".banner_slider").css({width:(banner_tot*banner_width)});
setTimeout( "slide()", 10000 );
});
function slide(){
if(current_num==(banner_tot)){
next_pos= 0;
$(".banner_slider").animate({left: next_pos}, speed);
current_num=1;
setTimeout( "slide()", 10000 );
}
else{
next_pos= current_num*(-banner_width);
$(".banner_slider").animate({left: next_pos}, speed);
current_num++;
setTimeout( "slide()", 10000 );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所看到的是动画的构建。当您切换到浏览器中的不同选项卡时,动画会放入队列中,当您切换回原始选项卡时,所有动画都会按顺序播放。
看看 http://www.learningjquery.com /2009/01/quick-tip-prevent-animation-queue-buildup
了解更多信息。
What you are witnessing is animation buildup. When you switch to a different tab in your browser, the animations get put into a queue, and when you switch back to the original tab, all of them are played sequentially.
Have a look at http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup
for more information.