Jump to content

Incrementing Javascript

Recommended Posts

Site URL: https://www.brianspoem.com

Hello!

I currently have a script running in the sidebar that counts the number of poems. It works, but the only problem is that I have to increment it manually. I asked a programmer friend if he could help me with a script that would count the number of poems from a starting date so that I would not have to manually edit the script each day. Below is the code he sent me. Unfortunately, I can't get this script to run and he is not able to help me. This script is very much like the script currently running, so I'm not sure what the problem is. Any insight or suggestions are greatly appreciated.

🙂 Brian

===================SCRIPT BELOW===================

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var a = 0;
$(window).scroll(function() {

  var oTop = $('#counter').offset().top - window.innerHeight;
  if (a == 0 && $(window).scrollTop() > oTop) {
    $('.counter-value').each(function() {
      var $this = $(this),
        startDate = getDaysSinceStartDate($this.attr('data-startdate'));
      $({
        countNum: $this.text()
      }).animate({
          countNum: startDate
        },
        {
          duration: 2000,
          easing: 'swing',
          step: function() {
            $this.text(Math.floor(this.countNum));
          },
          complete: function() {
            $this.text(this.countNum);
          }
        });
    });
    a = 1;
  }
function getDaysSinceStartDate(startDate) {
   var today = new Date().getTime() / 1000;
   startDate = Date.parse(startDate) / 1000;
   return Math.floor((today - startDate) / 60 / 60 / 24);
});
</script>

<div id="counter">
<div class="sqs-col sqs-col-4 counter-value" data-startdate="07/22/2016" data-desc="Poems
Published">0</div>
</div>

Link to comment
  • Replies 0
  • Views 438
  • Created
  • Last Reply

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.