Search code examples
javascriptdategreasemonkey

Greasemonkey if/else timeout with dates


I'm trying to write a script that refreshes a page every 10 seconds, but I want to have a different page every hour. Here's a simplified version of what I have so far using only two hour periods:

// ==UserScript==
// @name        RCS testing 12 - 21
// @namespace   cat1788@gmail.com
// @include     *
// @version     1
// @grant       none
// ==/UserScript==

setTimeout(function () {
  var today = new Date().getHours();
  //console.log(today);
  if (today >= 12 && today <= 13) {
      window.location.href = 'http://google.com';
  } else if (today >= 13 && today <= 14) {
      window.location.href = 'http://bing.com';
  }
}, 10000);

What's happening, though, is that only the first page ever starts on it's refresh cycle: what have I done wrong?

I'm new to Greasemonkey so maybe I've made a very basic mistake here, I hope you can help!

Thanks for your help, Cat


Solution

  • Thanks to @JaromandaX and @ShadowRanger, I don't need to use a period of time, I can just use the hour integer:

    setTimeout(function () {
      var hour = new Date().getHours();
      //console.log(today);
      if (hour === 12) {
          window.location.href = 'http://google.com';
      } else if (hour === 13) {
          window.location.href = 'http://bing.com';
      }
    }, 10000);