1
<html>
    <head>
    <title>Testing</title>


    </head>
        <body>
               <script type="text/javascript">
var target_date = new Date('Jan 04 2022 20:47:00').getTime();
var localMinDiff = new Date().getTimezoneOffset() * 60000;  
target_date = target_date - localMinDiff;
var days, hours, minutes, seconds;
        var countdown = document.getElementById('timeremaining');
        var countdownTimer = setInterval(function () {
            var current_date = new Date().getTime();
            var seconds_left = (target_date - current_date) / 1000;

            days = parseInt(seconds_left / 86400);
            seconds_left = seconds_left % 86400;

            hours = parseInt(seconds_left / 3600);
            seconds_left = seconds_left % 3600;
            minutes = parseInt(seconds_left / 60);
            seconds = parseInt(seconds_left % 60);

            if(days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0 )
            {
               countdown.innerHTML = 'time ended';
               clearInterval(countdownTimer);              
            }
            else
            {       
                if(days>0)
                  {
                     days= days+'d,';
                  } 
                  else
                  {
                     days='';
                  }         


                  countdown.innerHTML ='( ' + days + checkTime(hours) + ':'+ checkTime(minutes) + ':' + checkTime(seconds) +' remaining)';      
            }
        }, 1000);

        function checkTime(i) {
            if (i < 10) {i = '0' + i};  
            return i;
        }     
    </script>
     time remaining =

<div id="timeremaining"></div>
        </body>
        </html>

I am trying to run this reverse timer but it does display anything . I get an error message when i Log it. I also checked it with increasing timer duration but still i get an error.When i alert it i get the exact count down.

karthick prasad
  • 103
  • 2
  • 9

1 Answers1

0

The script needs to run AFTER the element is loaded. The issue is that your are calling innerHTML on the element that does not exist yet.

display name
  • 4,165
  • 2
  • 27
  • 52