calculate on
scrolling on
slow
normal
fast
Source Code
<script type="text/javascript">
var Prime = function() {
this.prime = 1;
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;
}
this.nextPrime = function() {
this.prime++;
while (!this.isPrime(this.prime)) this.prime++;
return this.prime;
}
}
this.prime = new Prime();
this.delay = 100;
function calcPrimesLoop() {
var primes = document.getElementById('primes');
primes.appendChild(document.createTextNode(' ' + this.prime.nextPrime()));
calcPrimesDelay = setTimeout('calcPrimesLoop()', this.delay);
}
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);
}
function calcPrimesStop() {
clearTimeout(calcPrimesDelay);
var calculate = document.getElementById('calculate');
calculate.href = 'javascript:calcPrimesStart()';
calculate.firstChild.data = 'calculate off';
scrollPrimesStop();
}
function scrollPrimesLoop() {
var primes = document.getElementById('primes');
primes.scrollTop = primes.scrollHeight;
scrollDelay = setTimeout('scrollPrimesLoop()', 100);
}
function scrollPrimesStart() {
var scroll = document.getElementById('scroll');
scroll.href = 'javascript:scrollPrimesStop()';
scroll.firstChild.data = 'scrolling on';
scrollPrimesLoop();
}
function scrollPrimesStop() {
clearTimeout(scrollDelay);
var scroll = document.getElementById('scroll');
scroll.href = 'javascript:scrollPrimesStart()';
scroll.firstChild.data = 'scrolling off';
}
function setDelay(delay) {
this.delay = delay;
}
window.onload = calcPrimesStart;
</script>
<textarea id="primes" rows="20"></textarea>
<a id="calculate">calculate on</a>
<a id="scroll">scrolling on</a>
<a id="slowSpeed" href="javascript:setDelay(1000)">slow</a>
<a id="normalSpeed" href="javascript:setDelay(100)">normal</a>
<a id="fastSpeed" href="javascript:setDelay(0)">fast</a>