Jump to content

Countdown script gets higher output after refreshing


Ryflex

Recommended Posts

Hi all,

I got a javascript of the net and integrated it within my php file.

After refreshing the page the timer gets higher everytime.

I echo'd the input variable on the same page and that gives the correct input in seconds.

Script:

<script type="text/javascript">
  var nIndex = '<?php echo "$time_difference"; ?>';   // 35 days, in seconds;
  var GMToffset = 0;     // your current GMT offset;
  var refDate = new Date();  // start date;


  refDate = new Date(refDate.getFullYear(),
         refDate.getMonth(),
         refDate.getDate(),
         refDate.getHours(),
         refDate.getMinutes(),
         refDate.getSeconds()+nIndex,
         0);  
  var done = false;
    
  function init(){

    var currDate = new Date();
    currDate.setHours(GMToffset+currDate.getHours()+currDate.getTimezoneOffset()/60);  
    if (refDate < currDate)
      {
       document.getElementById('display').innerHTML = "This auction is closed.";
       done = true;
      }
    var remTime = refDate-currDate;
    var nDays = parseInt(remTime/86400000);
    var nHours = parseInt((remTime-(nDays*86400000))/3600000);    
    var nMin = parseInt((remTime-(nDays*86400000)-(nHours*3600000))/60000);
    var nSec = 0;
    if (refDate > currDate)
      {
       nSec = 60-(currDate.getSeconds());
      }
    else   {
       nSec = currDate.getSeconds();
      }
    var nMonths = parseInt(nDays/30);
    var rDays = parseInt(nDays-(nMonths*30));    
    if (!done)
      {
       setTimeout("init()", 1000);
       document.getElementById('display').innerHTML = nMonths + " Months, " + rDays +
                  " Days, " + nHours + " Hours, " + nMin + " Minutes, " + nSec + " Seconds";
      }
  }

  navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);  

</script>
<style type="text/css">

   body {margin-top: 0px;}
  .timer_countdown {width: 460px; margin-left: auto; margin-right: auto;
    margin-top: 0px; text-align: center; font-family: 'times new roman';
    font-size: 12pt; color: #FFFFFF; background-color: #002b4d;
    padding-bottom: 3px;}
</style>
</head>
<body>
<center><h1><?php echo"$building" ?></h1></center>
<table width="500" border="1" align="center" cellpadding="2" cellspacing="0">
<tr>
<td><b>Building</b></td>
<td><b>Level</b></td>
<td><b>Upgrade</b></td>
</tr>
<tr>
<td><a href="link"><?php echo"$building" ?></a></td>
<td><?php echo "$building_level";?></td>
<td><div class="timer_countdown" id="display"></div></td>
<td><a href="link">Upgrade!</a></td>
</tr>
</table>

</body>

</html>

thnx

Ryflex

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.