JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-countto/1.2.0/jquery.countTo.js"></script>
<div class="empty-space">
scroll down...
</div>
<div class="stats">
<h1 data-from="200" data-to="2000" data-speed="1750" data-refresh-interval="50">200</h1>
</div>
<div class="empty-space">
</div>
CSS
.empty-space{
height: 500px;
}
JavaScript
$(function() {
var oTop = $('.stats').offset().top - window.innerHeight;
$(window).scroll(function(){
var pTop = $('body').scrollTop();
if( pTop > oTop ){
start_count();
}
});
});
var executed = false; // <= variable to false.
function start_count(){
if (!executed){ // <= make sure it didn't executed before.
$('.stats h1').countTo({
onComplete: function() {
var elementToPrepend = '<span style="margin-left:4px;">+</span>';
$(elementToPrepend).hide().appendTo(this).fadeIn();
}
});
executed = true; // <= the count() function already executed
}
}