我对 setInterval 的实现有疑问。我创建了一个 slider ,其中 setInterval 每隔几秒处理一个函数。我注意到在执行 setInterval 几分钟和几轮后会产生额外的延迟。 请提出这里的问题是什么?
$(document).ready(function() {
var totalItems = $('.sliderItem', '#slider').length;
var currentIndex = $('.itemActive').index() + 1;
var slideTime = 3000;
function goNext (e) {
$('.sliderItem').eq(e).fadeOut(500);
$('.welcomeBox > .welcomeText1', '.sliderItem').eq(e).hide(500);
$('h1', '.sliderItem').eq(e).hide(500);
$('h2', '.sliderItem').eq(e).hide(500);
if( e == totalItems - 1) {
e = 0;
} else {
e++;
};
$('.sliderItem').eq(e).fadeIn(400);
$('h1', '.sliderItem').eq(e).delay(800).show(400);
$('h2', '.sliderItem').eq(e).delay(500).show(400);
$('.welcomeBox > .welcomeText1', '.sliderItem').eq(e).delay(300).show(400);
currentIndex = e;
};
function loader() {
$('.loader').animate({"width":"100%"}, slideTime - 199);
$('.loader').animate({"opacity":"0"}, 199);
$('.loader').animate({"width":"0%"}, 0);
$('.loader').animate({"opacity":"1"}, 0);
};
function autoPlay (e){
timer = setInterval(function() {
loader();
goNext(e - 1);
console.log(e);
if( e == totalItems ) {
e = 1;
} else {
e++;
};
currentIndex = e;
}, slideTime);
};
autoPlay(currentIndex);
});
最佳答案
动画队列可能有问题。
我在使用 Chrome 或 webkit 浏览器时也遇到了类似的问题。将 setInterval/setTimeout 与 jQuery 的 .animate() 函数一起使用时。
同时打开 Original和 updated摆弄两个新选项卡并将其保留几分钟,然后再次检查。你会得到它更新的代码将流畅地动画,它与 stop
$('.sliderItem').eq(e).stop().fadeOut(500);
在fadeIn 或fadeOut 之前使用stop
jQuery 文档(source):
Because of the nature of requestAnimationFrame(), you should never queue animations using a setInterval or setTimeout loop. In order to preserve CPU resources, browsers that support requestAnimationFrame will not update animations when the window/tab is not displayed. If you continue to queue animations via setInterval or setTimeout while animation is paused, all of the queued animations will begin playing when the window/tab regains focus. To avoid this potential problem, use the callback of your last animation in the loop, or append a function to the elements .queue() to set the timeout to start the next animation.
$(document).ready(function() {
var totalItems = $('.sliderItem', '#slider').length;
var currentIndex = $('.itemActive').index() + 1;
var slideTime = 3000;
function goNext(e) {
$('.sliderItem').eq(e).stop().fadeOut(500);
$('.welcomeBox > .welcomeText1', '.sliderItem').eq(e).stop().hide(500);
$('h1', '.sliderItem').eq(e).stop().hide(500);
$('h2', '.sliderItem').eq(e).stop().hide(500);
if (e == totalItems - 1) {
e = 0;
} else {
e++;
};
$('.sliderItem').eq(e).stop().fadeIn(400);
$('h1', '.sliderItem').eq(e).stop().delay(800).show(400);
$('h2', '.sliderItem').eq(e).stop().delay(500).show(400);
$('.welcomeBox > .welcomeText1', '.sliderItem').eq(e).stop().delay(300).show(400);
currentIndex = e;
};
function loader() {
$('.loader').animate({
"width": "100%"
}, slideTime - 199);
$('.loader').animate({
"opacity": "0"
}, 199);
$('.loader').animate({
"width": "0%"
}, 0);
$('.loader').animate({
"opacity": "1"
}, 0);
};
function autoPlay(e) {
timer = setInterval(function() {
loader();
goNext(e - 1);
if (e == totalItems) {
e = 1;
} else {
e++;
};
currentIndex = e;
}, slideTime);
};
autoPlay(currentIndex);
});body {
background: black;
}
#slider {
position: relative;
width: 100%;
height: 100%;
color: #FFF;
padding: 30px;
}
#slider a {
color: #FFF;
}
.sliderItem {
position: absolute;
/* background: rgba(0, 0, 0, 0.28); */
display: none;
width: 100%;
padding: 57px;
margin: 0;
}
.sliderItem .welcomeText1 {
display: none;
}
.sliderItem h1,
.sliderItem h2,
.sliderItem h3,
.sliderItem>.welcomeBox>.welcomeText {
display: none;
}
.itemActive {
display: block;
}
.itemSelectors {
position: absolute;
bottom: 0;
display: block;
}
.itemSelectors>.selector {
background: #FFF;
color: #3b7cbc;
font-size: 18px;
line-height: 40px;
text-align: center;
font-weight: bold;
width: 40px;
height: 40px;
border-radius: 50%;
display: inline-block;
margin: 0 0 0 10px;
cursor: pointer;
}
.activeSelect {
background: #3a3a3a !important;
color: #FFF !important;
pointer-events: none;
}
.ms-nav-prev {
width: 30px;
background: url(https://xhosting.co.il/libraries/themes/xhosting/js/masterslider/skins/default/light-skin-1.png);
background-position: -89px -103px;
height: 40px;
cursor: pointer;
top: 50%;
right: 30px;
left: auto;
position: absolute;
z-index: 110;
}
.ms-nav-next {
width: 30px;
background: url(https://xhosting.co.il/libraries/themes/xhosting/js/masterslider/skins/default/light-skin-1.png);
background-position: -89px -26px;
height: 40px;
cursor: pointer;
top: 50%;
left: 30px;
position: absolute;
z-index: 110;
}
.loader {
position: absolute;
top: 0;
width: 0;
height: 10px;
background: rgba(255, 255, 255, 0.37);
}
.fadeInSlide {
animation: fadeInSlide 0.5s;
}
@-webkit-keyframes fadeInSlide {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id='slider'>
<div class='sliderItem itemActive'>
<div class="welcomeBox row">
<div class="col-md-4">
<div class="">
<h2 class="">ברוכים הבאים ל</h2>
<h1 class=''>HOST<span>1</span></h1>
</div>
</div>
<div class="welcomeText1 col-md-8">
<div class=''>
על מנת לספק את השירותים הללו באיכות הגבוהה ביותר אנו משתמשים בתשתיות האיכותית ביותר הן ברמת החומרה והן ברמת התוכנה. לא משנה על איזה מערכת אתה עובד אם השרת לא איכותי התוצאות בהתאם. לכן אנחנו לא מתפשרים וקונים את הרכיבים הטובים ביותר בשוק.
</div>
</div>
</div>
</div>
<div class='sliderItem'>
<div class="welcomeBox row">
<div class="col-md-4">
<div class="">
<h2 class="">ברוכים הבאים ל</h2>
<h1 class=''>HOST<span>2</span></h1>
</div>
</div>
<div class="welcomeText1 col-md-8">
<div class=''>
על מנת לספק את השירותים הללו באיכות הגבוהה ביותר אנו משתמשים בתשתיות האיכותית ביותר הן ברמת החומרה והן ברמת התוכנה. לא משנה על איזה מערכת אתה עובד אם השרת לא איכותי התוצאות בהתאם. לכן אנחנו לא מתפשרים וקונים את הרכיבים הטובים ביותר בשוק.
</div>
</div>
</div>
</div>
<div class='sliderItem'>
<div class="welcomeBox row">
<div class="col-md-4">
<div class="">
<h2 class="">ברוכים הבאים ל</h2>
<h1 class=''>HOST<span>3</span></h1>
</div>
</div>
<div class="welcomeText1 col-md-8">
<div class=''>
על מנת לספק את השירותים הללו באיכות הגבוהה ביותר אנו משתמשים בתשתיות האיכותית ביותר הן ברמת החומרה והן ברמת התוכנה. לא משנה על איזה מערכת אתה עובד אם השרת לא איכותי התוצאות בהתאם. לכן אנחנו לא מתפשרים וקונים את הרכיבים הטובים ביותר בשוק.
</div>
</div>
</div>
</div>
</div>
<div class='container'>
<div class='itemSelectors'></div>
</div>
<div class="clouds"></div>
<div class='ms-nav-prev'></div>
<div class='ms-nav-next'></div>
<div class='loader'></div>
如果您需要在使用 fadeIn 或 fadeOut(而不是更通用的动画函数)时清除队列,您需要显式设置两个 .stop() 参数为真。
关于javascript - SetInterval 在多轮执行后创建延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45860742/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试