admin管理员组文章数量:1327301
I need to count up from 1 to 60 but i want to count up with an easing, so for example it would go from 1 to 30 with a 100ms delay, after that i need to increase the delay so the counting will gradually slow down when it reaches 60. This is what i got so far(not much):
var i = 0;
var interval = setInterval(function(){
i += 1;
console.log(i);
if(i == 60) {
clearInterval(interval);
}
}, 100);
I need to count up from 1 to 60 but i want to count up with an easing, so for example it would go from 1 to 30 with a 100ms delay, after that i need to increase the delay so the counting will gradually slow down when it reaches 60. This is what i got so far(not much):
var i = 0;
var interval = setInterval(function(){
i += 1;
console.log(i);
if(i == 60) {
clearInterval(interval);
}
}, 100);
Share
Improve this question
asked Nov 27, 2013 at 10:35
passatgtpassatgt
4,4425 gold badges43 silver badges56 bronze badges
2
- 1 see jsfiddle/arunpjohny/7xfCg/1 ? – Arun P Johny Commented Nov 27, 2013 at 10:39
- almost, but i need to slow down with an easing, like an animation: gsgd.co.uk/sandbox/jquery/easing – passatgt Commented Nov 27, 2013 at 10:47
3 Answers
Reset to default 5I'd use setTimeout()
, something like this:
var delay = 100, count = 0;
function delayed () {
count += 1;
console.log(count);
if (count > 30) {
delay += 10;
}
if (count < 60) {
setTimeout(delayed, delay);
}
}
delayed();
A live demo at jsFiddle.
Why not slow down continuously? Looks much nicer in my opinion. I updated the answer of @Teemu accordingly. See this fiddle.
var delay = 0, count = 0;
function delayed () {
count += 1;
console.log(count);
//adjust to influence overall speed
delay+=5;
if (count <60) {
setTimeout(delayed, delay);
}
}
Here's something you can use. A bit of mathematics in action
var i = 0, a = 0;
var interval = setInterval(function(){
i += 1;
a = parseInt(Math.log(i)*Math.log(i)*100, 10);
print(i, a);
if(i == 60) {
clearInterval(interval);
}
}, 100);
function print(i, a){
setTimeout(function(){
console.log(i)
},a);
}
Basically, the value of a
will increase gradually. You can observe very minor difference in time the count of i
prints
本文标签: jqueryHow to increase javascript loop delay with easingStack Overflow
版权声明:本文标题:jquery - How to increase javascript loop delay with easing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742205170a2432680.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论