根据时间重新加载网页

时间:2017-02-23 23:49:38

标签: javascript html

这是我的问题

的代码
<script>var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(hour <= 2){ 
    document.getElementById("p").innerHTML = "wyd?";    
    window.reload();
}
if(hour > 2 && hour <= 3){
    document.getElementById("p").innerHTML = "xxx did nothing wrong";
}
if(hour  > 3 && hour <= 4 ){
    document.getElementById("p").innerHTML = "The holocaust was a hoax!";
}
if(hour  > 4 && hour <= 5 ){
    document.getElementById("p").innerHTML = "dogs r gr8";
}
if(hour  > 5 && hour <= 6){
    document.getElementById("p").innerHTML = "meme";
}
if(hour  > 6 && hour <= 7 ){
    document.getElementById("p").innerHTML = "good morning u fuckin legend";
}
if(hour > 7 && hour  <= 8){ 
    document.getElementById("p").innerHTML = "Big ol'Floofer";
}
if(hour  > 8 && hour <= 9 ){
    document.getElementById("p").innerHTML = "Hey fucktard here's the weather";
}
if(hour  > 9 && hour <= 10 ){
    document.getElementById("p").innerHTML = "rip @xxxx";
}
if(hour  > 10&& hour <= 11 ){
    document.getElementById("p").innerHTML = "69";
}
if(hour  > 11 && hour <= 12 ){
    document.getElementById("p").innerHTML = "Time 2 meme";
}
if(hour  > 12 && hour <= 13 ){
    document.getElementById("p").innerHTML = "cash me outside?";
}
if(hour  > 13 && hour <= 14 ){
    document.getElementById("p").innerHTML = "Harambe still lives!";
}
if(hour  > 14 && hour <= 15 ){
    document.getElementById("p").innerHTML = "greasy randy!";
}
if(hour  > 15 && hour <= 16 ){
    document.getElementById("p").innerHTML = "made by @xxx";
}
if(hour  > 16 && hour <= 17 ){
    document.getElementById("p").innerHTML = "sponsered by busch";
}
if(hour  > 17 && hour <= 18 ){
    location.reload();
    document.getElementById("p").innerHTML = "bork";
}
if(hour  > 18 && hour <= 19 ){
    document.getElementById("p").innerHTML = "sweet doggo";
}
if(hour  > 20 && hour <= 21 ){
    document.getElementById("p").innerHTML = "floof";
}
if(hour  > 21 && hour <= 22 ){
    document.getElementById("p").innerHTML = "get a good sleep u";
}
if(hour  > 22 && hour <= 23 ){
    document.getElementById("p").innerHTML = "bruh";
}
if(hour  > 23 && hour <= 24 ){
    document.getElementById("p").innerHTML = "There goes ur good sleep";
}

我还有其他这样的行,每行都有不同的时间,在x和x的小时之间输出不同的短语。当我运行程序(网站)它不断重新加载页面时,我想知道如何获取它所以它只重新加载网页一次而不是像它正在做的那样连续。 理想情况下,这是完全没有用户输入(例如按钮)的自动化。

1 个答案:

答案 0 :(得分:0)

我猜你想要一个显示每小时更改消息的页面?如果是这样,我会将messages存储在数组中以保持整洁,然后使用setTimeout每小时刷新一次消息。

function checkMessage(){
  var d = new Date(), h = d.getHours(), m = d.getMinutes();
  document.getElementById('p').innerHTML = messages[h];
  setTimeout(checkMessage, (60 - m) * 1000);
}

window.addEventListener('load', checkMessage);