There are 2 functions below- myCalculator() function which holds the variable with static value and animateValue() function will animate that value from var start to end.(defined var in 1st func.) But the issue is that the value is not animating on form button click
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form id="contact" action="" method="post">
<fieldset>
<font class="label">How many people do you want to save per month? </font> <input placeholder="" id="active-member" type="text" style="width:110px;float: right;"/>
</fieldset>
<fieldset>
<button name="submit" type="button" id="contact-submit" onclick="myCalculator();" style="float: right;">Calculate</button>
</fieldset>
</form>
<div class="container2" style="display:none;">
Animating Value <span id="value" ></span>
</div>
<script>
function myCalculator() {
$(".container2").show();
var start = 1000;
var end = 23600191;
animateValue("value", start, end, 1000);
}
function animateValue(id, start, end, duration) {
var range = end - start;
var current = start;
var increment = end > start? 1 : -1;
var stepTime = Math.abs(Math.floor(duration / range));
var obj = document.getElementById(id);
var timer = setInterval(function() {
current += increment;
obj.innerHTML = current;
if (current == end) {
clearInterval(timer);
}
}, stepTime);
}
</script>
Note:
- animateValue function is not taking the value of
var start and end - 2000 is 2 second
- myCalculate function calculating some values
- animateValue function will animating the value
startandendparameters when you callanimateValue. Worked fine when I added any values to it. Check this fiddle jsfiddle.net/e1xd4ru1/1animateValuefunction frommyCalculatefunction itself. Or post some more code to explain what exactly your scenario is.var incrementvalue from 1 to any number then it will not work. It continuing the animation to infinite number. And Its very important to set thevar incrementvaluevar incrementvalue and try to change it and see..