I have function countup.
Below is my code and demo.
Now I need to stop the countup but I don't have any idea how to put and do that on function.
So the idea, I just call this: $("#a").countRunTime().stop() then it will stop the countup.
Any idea?
$.fn.countRunTime = function(fromDate){
var $el = this;
tick();
var options = $.extend({
callback: function() {}
}, arguments[0] || {});
options.callback.call(this);
function formatTime(distance) {
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
var value = (hours > 9 ? hours : '0' + hours) + ":" + (minutes > 9 ? minutes : '0' + minutes) + ":" + (seconds > 9 ? seconds : '0' + seconds);
return value;
}
function tick() {
var now = new Date().getTime();
if(fromDate > now) {
$el.html("Invalid");
}
else {
var remaining = now-fromDate;
$el.html(formatTime(remaining));
setTimeout(tick, 1000);
}
};
};
$("#a").countRunTime(new Date("20 Jul 2022 11:21:33").getTime());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="a"></div>