The value of i should increment to 500 in intervals of 5 seconds. But I need the previous day's total to be added to the following day. For simplicity now, I just need to add the max value prior to the start of the daily incrementing of i. However i is initializing at 0 for the incrementing and will reset regardless of max being added to it. 

What is the best way to go about this?
Here's my code so far.

```
 <script type='text/javascript'>
    var div = document.getElementById('count');
    var i = 0; 
    var count = 0;
    var max = 500;
    var d = new Date();
    // set d to midnight
    d.setHours(0,0,0,0);
    d.setMonth(0);
    d.setDate(1);
    d.setFullYear(2017);
     var one_day=1000*60*60*24;
     var today = new Date().getTime();  
     var numberofdays = Math.round((today - d.getTime()) / one_day);

   function isPastMidnight() {
      var currentTime = new Date().getTime();
      if ((currentTime - d.getTime()) > 0) {
        return true;
      } else {
        return false;
      } 
    }
    function updateCount() {
      var currentTime = new Date().getTime();
      if (!isPastMidnight()) {
       document.getElementById('count').innerHTML = "Check back January 1, 2017.";
      }

     if (isPastMidnight())   {
      if (numberofdays > 0 && today == d.getHours)   {      
        addmax();

        }
      }
     if (isPastMidnight() && i < max) {
        i = Math.floor((currentTime - d.getTime() ) / 5000);
        if (i > max) i = max; 
        document.getElementById('count').innerHTML = i;
      }
        if (i >= max) {
        // Stop executing myself ("interval" is declared in global scope, so accessible from here)
        clearInterval(interval);
      }
    }
    updateCount();
    var interval = setInterval(updateCount, 1000);
</script>

```