Skip to content

Instantly share code, notes, and snippets.

@Rupashdas
Last active March 19, 2024 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rupashdas/80e7a98d675af1fc3201720c2bef6e6b to your computer and use it in GitHub Desktop.
Save Rupashdas/80e7a98d675af1fc3201720c2bef6e6b to your computer and use it in GitHub Desktop.
function countDown(selector) {
var countDownElement = $(selector);
var startAt = parseFloat(countDownElement.attr('start-at'));
var endAt = parseFloat(countDownElement.attr('end-at'));
var animationDuration = parseFloat(countDownElement.attr('animation-duration').replace('ms', ''));
var currentValue = startAt;
var interval = 50;
var steps = (startAt - endAt) / (animationDuration / interval);
var timer = setInterval(function () {
currentValue -= steps;
countDownElement.text(currentValue.toFixed(2));
if (currentValue <= endAt) {
clearInterval(timer);
countDownElement.text(endAt.toFixed(2));
}
}, interval);
}
let countDownSelectors = ".count-down";
if ($(countDownSelectors).length > 0) {
$(countDownSelectors).each(function (index, item) {
var waypoint = new Waypoint({
element: $(this),
handler: function () {
countDown(item);
this.destroy();
},
offset: 'bottom-in-view',
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment