On Not | Mo Chit

May 28, 2004

Flash: Fun with setInterval If you've ever written your own scrollbar you've had to deal with the behavior of clicking on the up or down arrow which causes an immediate scroll, followed by a pause and then continually scrolling.

So typically you would want to call a scrollDown method for example immediately, then setup an interval for the pause, and then setup another interval for the continuous scrolling.

Usually your code would look something like this:
function onPress() {
  //scroll immediately
  scrollDown();
 //setup pause
 _scrollID = setInterval(this,"initDownScroll",350);
}

function initDownScroll() {
 clearInterval(_scrollID);
 //scroll immediately again
 scrollDown();
 //setup continuous scrolling
 _scrollID = setInterval(this,"scrollDown",50);
}

function scrollDown() {
 //handle scrolling here
}
Thankfully there is a easy fix that lets you only have to use one function for both intervals:
function onPress() {
  //scroll immediately
 scrollDown();
 //setup pause
 _scrollID = setInterval(this,"scrollDOwn",350,true);
}

function scrollDown(first:Boolean) {
  if(first) {
    clearInterval(_scrollID);
    //setup continuous scrolling
    _scrollID = setInterval(this,"scrollDown",50);
  }

  //handle scrolling here
}
So just by passing an extra parameter to the first interval, the scrollDown function can detect that the pause is over and setup the next interval.

Creative Commons License
This site is licensed under a
Creative Commons License