calculate on    scrolling on    slow    normal    fast

Source Code

<script type="text/javascript" src="ipnum.js"></script>
<script type="text/javascript">
  //<![CDATA[

  // calculate successive fibonacci numbers
  var Fibonacci = function() {
      // current fibonacci sequence
      this.fib1 = new IpNum();
      this.fib1.setString('0');
      this.fib2 = new IpNum();
      this.fib2.setString('1');

      // return next fibonacci number
      this.next = function() {
          var x = new IpNum();
          x.setArray(this.fib1.getArray());
          this.fib1.setArray(this.fib2.getArray());
          this.fib2.setArray(this.fib2.add(x).getArray());
          return x.getString();
      }
  }

  // fib
  this.fib = new Fibonacci();

  // current delay
  this.delay = 100;

  // calculate fibonacci numbers
  function calcFibonacciLoop() {
      var fibonaccis = document.getElementById('fibonaccis');
      fibonaccis.appendChild(document.createTextNode(' ' + fib.next()));
      calcDelay = setTimeout('calcFibonacciLoop()', this.delay);
  }

  // start calculating fibonacci numbers
  function calcFibonacciStart() {
      var fibonaccis = document.getElementById('fibonaccis');
      var spacer = ' ';
      if (fib.fib1.getString() == '0') spacer = '';
      fibonaccis.appendChild(document.createTextNode(spacer + fib.next()));
      var calculate = document.getElementById('calculate');
      calculate.href = 'javascript:calcFibonacciStop()';
      calculate.firstChild.data = 'calculate on';
      scrollFibonacciStart();
      calcDelay = setTimeout('calcFibonacciLoop()', this.delay);
  }

  // stop calculating fibonacci numbers
  function calcFibonacciStop() {
      clearTimeout(calcDelay);
      var calculate = document.getElementById('calculate');
      calculate.href = 'javascript:calcFibonacciStart()';
      calculate.firstChild.data = 'calculate off';
      scrollFibonacciStop();
  }

  // scroll fibonacci text area
  function scrollFibonacciLoop() {
      var fibonaccis = document.getElementById('fibonaccis');
      fibonaccis.scrollTop = fibonaccis.scrollHeight;
      scrollDelay = setTimeout('scrollFibonacciLoop()', 100);
  }

  // start scrolling fibonacci text area
  function scrollFibonacciStart() {
      var scroll = document.getElementById('scroll');
      scroll.href = 'javascript:scrollFibonacciStop()';
      scroll.firstChild.data = 'scrolling on';
      scrollFibonacciLoop();
  }

  // stop scrolling fibonacci text area
  function scrollFibonacciStop() {
      clearTimeout(scrollDelay);
      var scroll = document.getElementById('scroll');
      scroll.href = 'javascript:scrollFibonacciStart()';
      scroll.firstChild.data = 'scrolling off';
  }

  // set delay
  function setDelay(delay) {
      this.delay = delay;
  }

  window.onload = calcFibonacciStart;

  //]]>
</script>

<textarea id="fibonaccis" rows="20"></textarea>

&nbsp;&nbsp;
<a id="calculate">calculate on</a>
&nbsp;&nbsp;
<a id="scroll">scrolling on</a>
&nbsp;&nbsp;
<a id="slowSpeed" href="javascript:setDelay(1000)">slow</a>
&nbsp;&nbsp;
<a id="normalSpeed" href="javascript:setDelay(100)">normal</a>
&nbsp;&nbsp;
<a id="fastSpeed" href="javascript:setDelay(0)">fast</a>