I'm writing an animation looks like number is rising in limited time. use setInternal and clearInterval to implement it.(I don't want to use Jquery) the interval time is 1 milliSecond. And limit time is 1000 milli seconds. But it is slower than expected. It went almost 10000 milliseconds.What am I doing wrong?
Here's my code.
<html>
<head>
<title>A numeric test</title>
</head>
<body>
<div id="numberScroll"></div>
<script type="text/javascript">
var limitTime = 100; //milliSeconds
var goalNum = 123456; //the final num
var increament = false; //whether the animation is increasing of not.
var textSize = 30; //size of text
var temNum = increament ? (goalNum * 0.8) : (goalNum * 1.2); //start number
temNum = eval(temNum.toFixed(2));
var increamentNum = (goalNum - temNum) / limitTime;
var numberScroll = document.getElementById("numberScroll");
if(numberScroll === null) {
console.log("No Element Found");
}
numberScroll.style.fontSize = textSize;
numberScroll.innerHTML = temNum;
var intervalId = setInterval(function(){
numberScroll.innerHTML = temNum.toFixed(2);
temNum += increamentNum;
if(increament){
if(temNum >= goalNum) {
clearInterval(intervalId);
numberScroll.innerHTML = goalNum;
}
} else {
if(temNum <= goalNum) {
clearInterval(intervalId);
numberScroll.innerHTML = goalNum;
}
}},1);
</script>
</body>
</html>