1
function startAutoScrolling() {

     var distance = y2 - y1;
     var speed = distance / dateDiff;

     interval1 = setInterval(function() { doAutoScrolling(speed); }, 1); 

}

here I want to decrease step on 0.1 but it dosn't work like that, and I don't know why

function doAutoScrolling(step) {

     document.getElementById(CONTEINER).scrollTop += step;

     if (isGoingDown) {
          step  = step - 0.1;
          console.log("step - 1: " + step); 
          if (step <= 0) {
               clearInterval(interval1); 
          }
     } else  {   // there is continue below

here I want to increase step and if condition have to stop execution of block but it doesn`t work also

          step += 0.01;
          if (step >= 0) {
               clearInterval(interval1); 
          }
     } 
}

1 Answers1

4

You're using the Javascript comma-operator in place where you most probably want to use a decimal, e.g.:

step  = step - 0,1;

Should be:

step  = step - 0.1;

More about the comma-operator:

UPDATE (after commas to dots -change)

Primitives are passed-by-value in Javascript (see: Does Javascript pass by reference?) so you're basically calling doAutoScrolling over and over again with the same value (the value of speed). You need to do one of the following:

  • wrap speed in an object in order to pass-by-reference
  • make speed a global variable or at least defined in the parent context of doAutoScrolling
  • Replace setInterval with setTimeout and set a new timeout in doAutoScrolling:

    var newStep = /* calculate new step */ setTimeout("doAutoScrolling("+newStep+")",1);

Community
  • 1
  • 1
heikkim
  • 2,835
  • 2
  • 21
  • 33