简体   繁体   中英

Automatically refreshing page every 60 seconds using JavaScript

I am developing a simple digital clock and I want the page to reload every 60 seconds.

So basically when the minutes on the clock change I want the page to refresh automatically.

Below is my JS Code:

var date = new Date();
var h = date.getHours();
var m = date.getMinutes();
var s = date.getSeconds();

if (s == 60){   //If the seconds equal to 60 then update the minutes accordingly
  m++; 
}

window.onload = function(){
  if (h >= 12) {
    if(m < 10){
      var timePM = ' PM';
      document.getElementById('time').innerHTML =   h + ':' + '0' + m + timePM;
    } else{
      var timePM = ' PM';
      document.getElementById('time').innerHTML =   h + ':' + m + timePM;
    }
  }
  else{
    if(m<10){
      var timeAM = ' AM';
      document.getElementById('time').innerHTML =   h + ':' + + '0' + m + timeAM;
    }else{
      var timeAM = ' AM';
      document.getElementById('time').innerHTML =   h + ':' + m + timeAM;
    }
  }
}

I have tried using the following meta tag. However that did not seem to work.

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

Try this and you're done...

setInterval(function(){
   window.location.reload(1);
}, 60000);

setTimeout function call only once so use setInterval function

setInterval("location.reload(true);", 60000);

it will refresh your page after one minute.

If you need to write a digital clock. Here is a good sample :

https://codepen.io/afarrar/pen/JRaEjP

And this line of code will refresh your content:

setTimeout("Your String", 60000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM