calculate on    scrolling on    slow    normal    fast

Source Code

<script type="text/javascript">
  //<![CDATA[

  // calculate successive prime numbers
  var Prime = function() {
      // current prime number
      this.prime = 1;

      // return true if NUM is prime
      this.isPrime = function(num) {
          var result = true;
          if (num !== 2) {
              if (num % 2 == 0) {
                  result = false;
              } else {
                  for (x = 3; x <= Math.sqrt(num); x += 2) {
                      if (num % x == 0) result = false;
                  }
              }
          }
          return result;
      }

      // return next prime number
      this.nextPrime = function() {
          this.prime++;
          while (!this.isPrime(this.prime)) this.prime++;
          return this.prime;
      }
  }

  // prime
  this.prime = new Prime();

  // current delay
  this.delay = 100;

  // calculate primes
  function calcPrimesLoop() {
      var primes = document.getElementById('primes');
      primes.appendChild(document.createTextNode(' ' + this.prime.nextPrime()));
      calcPrimesDelay = setTimeout('calcPrimesLoop()', this.delay);
  }

  // start calculating primes
  function calcPrimesStart() {
      var prime = this.prime.nextPrime();
      var calculate = document.getElementById('calculate');
      calculate.href = 'javascript:calcPrimesStop()';
      calculate.firstChild.data = 'calculate on';
      var primes = document.getElementById('primes');
      var spacer = ' ';
      if (prime == 2) spacer = '';
      primes.appendChild(document.createTextNode(spacer+prime));
      scrollPrimesStart();
      calcPrimesDelay = setTimeout('calcPrimesLoop()', this.delay);
  }

  // stop calculating primes
  function calcPrimesStop() {
      clearTimeout(calcPrimesDelay);
      var calculate = document.getElementById('calculate');
      calculate.href = 'javascript:calcPrimesStart()';
      calculate.firstChild.data = 'calculate off';
      scrollPrimesStop();
  }

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

  // start scrolling primes text area
  function scrollPrimesStart() {
      var scroll = document.getElementById('scroll');
      scroll.href = 'javascript:scrollPrimesStop()';
      scroll.firstChild.data = 'scrolling on';
      scrollPrimesLoop();
  }

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

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

  window.onload = calcPrimesStart;

  //]]>
</script>

<textarea id="primes" 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>