1

I am using the following code in my website which displays the current time

function startTime() {
        var today = new Date();
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        m = checkTime(m);
        s = checkTime(s);
        document.getElementById('time').innerHTML =
                 h + ":" + m;
        var t = setTimeout(startTime, 500);
    }
    function checkTime(i) {
        if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
        return i;
    }

i am also using the automatic refresher tag in my html which reloads page after every 60 seconds

<meta http-equiv="refresh" content="60">

what i want is whenever the time changes to next minute the page reloads which means if current time is 14:05 and when it hits 14:06 the page reloads by reading this time change and NOT by 60 seconds interval from which the user opens the page.

Garry
  • 342
  • 5
  • 18

3 Answers3

2

You can set timeout looking at the clock, just get the actual seconds and wait til 60 to reload:

var date = new Date();
setTimeout(function(){
    window.location.reload(1);
},(60 - date.getSeconds())*1000)

Just put that at the head inside a script tag

Mark E
  • 3,403
  • 2
  • 22
  • 36
1

Try using this

setTimeout(function(){
    window.location.reload(1);
}, 60000); // 60 sec

Source: How to reload page every 5 second?

or take a look at this too

setTimeout(function(){
    var minutes = (new Date()).getMinutes()
    if ( !minutes%15 ) location.reload(); // if minutes is a multiple of 15

},60000); // 60.000 milliseconds = 1 minute

Source: jQuery auto refresh page on clock time

Community
  • 1
  • 1
Max Maxymenko
  • 551
  • 1
  • 5
  • 13
1

Handling the local time using client side script is not recommended because the user's clock might be messed up and thus your system would turn out to be faulty.

So it is better you fetch time from your server using any server-side language like PHP

In PHP:

<?php
   echo date("h:i");
?>

Now you can call this function using AJAX and you can easily handle your time.

var result=null;
function getDate(){
    var result=$.ajax({
          url: "script.php",
          type: "POST",
          success: function(data){
              setTimeOut(function(){getDate();},60000);
          }
    }).responseText;
}
Utkarsh Dhawan
  • 324
  • 3
  • 9