简体   繁体   English

在循环内对setTimeout延迟应用缓动

[英]Applying easing to setTimeout delays, within a loop

I am calling multiple setTimeout's within a javascript loop. 我在javascript循环中调用了多个setTimeout。 The delay is currently set to increase by 200ms upon every iteration making the 'self.turnpages()' function fire every 200ms. 目前,每次迭代都会将延迟设置为增加200毫秒,使“ self.turnpages()”函数每200毫秒触发一次。

However I would like to apply some sort of easing to these variable delays so that as the loop begins to reach the last few iterations the delay gets further apart causing the function firing to slow down. 但是,我想对这些可变延迟应用某种缓和,以便当循环开始到达最后几次迭代时,延迟会进一步分开,从而导致函数触发速度变慢。

var self = this;    
var time = 0; 

for( var i = hide, len = diff; i < len; i++ ) {
                     (function(s){
                             setTimeout(function(){                    
                                        self.turnPages(s);                           
                             }, time);
                       })(i);                                  
             time = (time+200);
}

I am at a complete loss how to start with this. 我完全不知道如何开始。

Hopefully someone can help. 希望有人可以提供帮助。

This sounds like a job for Robert Penner's easing equations! 这听起来像是罗伯特·彭纳的缓和方程式的工作! You can download the original ActionScript 2.0 versions here (just remove the strong-typing on the parameters to port to JavaScript) and there's a good explanation of the parameters here . 您可以在此处下载原始的ActionScript 2.0版本(只需删除参数的强类型以移植到JavaScript),并在此处对参数进行了很好的说明。

Something like the following will do what you want ( fiddle ): 像下面这样的东西会做你想要的( 小提琴 ):

var time = 0;
var diff = 30;

var minTime = 0;
var maxTime = 1000;

// http://upshots.org/actionscript/jsas-understanding-easing
/*
    @t is the current time (or position) of the tween. This can be seconds or frames, steps, seconds, ms, whatever – as long as the unit is the same as is used for the total time [3].
    @b is the beginning value of the property.
    @c is the change between the beginning and destination value of the property.
    @d is the total time of the tween.
*/
function easeInOutQuad(t, b, c, d) {
  if ((t /= d / 2) < 1) return c / 2 * t * t + b;
  return -c / 2 * ((--t) * (t - 2) - 1) + b;
}

function easeOutQuad(t, b, c, d) {
  return -c * (t /= d) * (t - 2) + b;
}

function easeInQuad(t, b, c, d) {
  return c * (t /= d) * t + b;
}

for (var i = 0, len = diff; i <= len; i++) {
  (function(s) {
    setTimeout(function() {
      //self.turnPages(s);                           
      console.log("Page " + s + " turned");
    }, time);
  })(i);

  time = easeInOutQuad(i, minTime, maxTime, diff);
  console.log(time);
}

2019 answer : You don't need to make your own loop timings, or deal with single character variables, just to run functions with easing. 2019年答案 :您无需自行制定循环计时或处理单个字符变量,只需轻松运行函数即可。 Here's a quick example using easy-ease from npm: 这是一个使用npm中的easy-ease的简单示例:


import ease from 'easy-ease';

ease({
  startValue: 1,
  endValue: 33,
  durationMs: 2000,
  onStep: function(value) {
    console.log(value)
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM