I have a function called: getNum() that returns a number added to itself plus 0.01 and displays it.
Then there is update() which checks if the number has crashed, and if not, it tests it, and if it doesn't crash, it sets the loop interval to be faster than it was before by a factor of 8ms.
So the result is that the number gets bigger faster and faster.
My question is: Why, when the number crashes, does it properly alert "crashed", but it doesn't clearInterval and the number keeps going?
<html>
<body>
<p id="output"></p>
</body>
</html>
<script type="text/javascript">
var x = 0,
loop = 2000;
var interval = setInterval(getNum,loop),
interval1 = setInterval(update,100),
crashChance = 150,
crash = 0;
function getNum() {
x = x + 0.01;
x = Math.floor(x * 100) + 1;
x = x / 100;
document.getElementById('output').innerHTML = x;
}
function update() {
testCrash();
if(testCrash() == 1) {
alert("crashed");
clearInterval(interval);
clearInterval(interval1);
crashChanse = 0;
crash = 0;
}
else {
loop = loop - 8;
interval = setInterval(getNum,loop);
}
}
function testCrash() {
crash = Math.floor(Math.random() * 150 + 1);
if(crash == crashChance) {
return 1;
}
else {
return 0;
}
}
</script>
<html>
<body>
<p id="output"></p>
</body>
</html>
<script type="text/javascript">
var x = 0,
loop = 2000;
var interval = setInterval(getNum,loop),
interval1 = setInterval(update,100),
crashChance = 150,
crash = 0;
function getNum() {
x = x + 0.01;
x = Math.floor(x * 100) + 1;
x = x / 100;
document.getElementById('output').innerHTML = x;
}
function update() {
testCrash();
if(testCrash() == 1) {
alert("crashed");
clearInterval(interval);
clearInterval(interval1);
crashChanse = 0;
crash = 0;
}
else {
loop = loop - 8;
interval = setInterval(getNum,loop);
}
}
function testCrash() {
crash = Math.floor(Math.random() * 150 + 1);
if(crash == crashChance) {
return 1;
}
else {
return 0;
}
}
</script>