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.
Posted by Wes at 12:30 PM
Flash: Short circuit evaluation After seeing Darron Schall's white space revelation the other day, I thought I would share something else common to most programming languages. In Actionscript logical expressions are evaluated executing the least amount of comparisons necessary to evaluate the expression. For example
if(true || complexFunction()) ...
if(false && complexFunction()) ...
In both examples complexFunction() will never be executed since it is unnecessary to evaluate the expression. So if your looking for that extra bit of performance, it's always good practice when forming potentially intensive logical comparisons, to put the least complicated comparisons first.
Posted by Wes at 12:05 PM
Bravo, Sony. sony_pva.jpg I take back most of the bad things I said about Sony in my last Sony post. Clearly they have their heads screwed on right when they came up with the VGF-AP1. Finally, people will be able to carry around something other than an iPod without holding their heads in shame. Now, all I have to figure out is what I'll have to pawn in order to pay for this new, pricey gadget. Read more about it at The Register.
Posted by Dudley at 02:16 AM

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