javascript - Ease time between firing specific number of timeouts in a specific period of time -


it's kind of math problem. want fire specific number of settimeout (the number based on array length) in specific period of time (say, 5 seconds).

the first settimeout should start @ 0 sec. , last @ 5 sec.. timeouts between should start ease-in effect, each timeout starts faster.

there's example ilustrates want achieve exactly.
i'm struggling around line:

next += timeperiod/3.52/(i+1); 

which works perfect in demo example (for timeperiod), doesn't work different letters.length have used static number 3.52.

how calculate next?

var letters = [     'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'  ];    var div        = $('#container');  var timeperiod = 5000; // 5 seconds;  var perletter  = timeperiod/(letters.length-1); // gives equal time between letters  var next       = 0;    for(var i=0; i<letters.length; i++){    settimeout(function(letter){      //div.append('<span class="letter">' + letter + '</span>');      // used "|" instead of letter, better redability:      div.append('<span class="letter">|</span>');    }, next, letters[i]);        // can't find logic here:     next += timeperiod/3.52/(i+1);      };      ///////////////// demo: ///////////////    var sec = timeperiod/1000;  var secondsinterval = setinterval(seconds, 1000);    var demointerval = setinterval(function(){    sec >= 0 || clearinterval(demointerval);    div.append('\'');  }, 30);    function seconds(){    sec || clearinterval(secondsinterval);    $('#sec').text(sec-- || 'done');  }  seconds();
.letter{    color : red;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <span id=container></span>  <span id=sec class=letter></span>

var steps = letters.length; var target = timeperiod;  function easeoutquad(t, b, c, d) {   t /= d;   return -c * t*(t-2) + b; };  var arrayoftimeouts = new array(steps); var n; var prev = 0; for(var = 1; <= steps; i++){   n = easeoutquad(i, 0.0, target, steps);   arrayoftimeouts[i-1] = n-prev;      prev = n; } 

this 1 should work input value.

fiddle

note graph appears fast believe discrepancy product of timing imperfections, sum of array equals timeperiod exactly.

more on easing equations


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -