daily pastebin goal
92%
SHARE
TWEET

XKCD Time

a guest Mar 26th, 2013 547 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. [code]
  3. function () {
  4.     function t(e) {
  5.         (new Image).src = "http://xkcd.com/events/" + e
  6.     }
  7.     function n() {
  8.         location.hash == "#verbose" && console.log.apply(console, arguments)
  9.     }
  10. // This spreads requests over 8 subdomains
  11.     var e = ["http://c0.xkcd.com", "http://c1.xkcd.com", "http://c2.xkcd.com", "http://c3.xkcd.com", "http://c4.xkcd.com", "http://c5.xkcd.com", "http://c6.xkcd.com", "http://c7.xkcd.com"];
  12.     try {
  13. // This selects randomly between the subdomains above
  14.         var r = e[Math.floor(Math.random() * e.length)],
  15. // These create a new event source to listen for a new comic
  16.             i = r + "/stream/comic/time?method=EventSource",
  17.             s = new EventSource(i);
  18. // This starts a connection to the event source
  19.         n("connecting to event source:", i), s.addEventListener("open", function (e) {
  20.             t("connect_start")
  21.         }, !1), s.addEventListener("error", function (e) {
  22.             n("connection error", e), t("connect_error")
  23.         }, !1), s.addEventListener("comic/time/ping", n, !1), s.addEventListener("comic/time", n, !1);
  24.         var o = !0;
  25. // If the event source spits out a response, parse it.
  26. // The event source is just a JSON file that includes what looks like a SHA1 hash that is used to construct the image's file name
  27.         s.addEventListener("comic/time", function (e) {
  28.             var t = JSON.parse(e.data),
  29.                 r = document.getElementById("comic").getElementsByTagName("img")[0],
  30.                 i = o ? 0 : Math.round(Math.random() * t.spread);
  31.             n("waiting", i, "seconds before displaying comic", t.image), setTimeout(function () {
  32. // This changes the image on the web page
  33.                 r.src = "http://imgs.xkcd.com/comics/time/" + t.image
  34.             }, i * 1e3), o = !1
  35. // This basically just keeps checking for a new image, and if it finds one, loads it.
  36. // The timing of new images is entirely server-side, controlled by what JSON response the servers give.
  37. // As far as I can see, there is no way to speed things up, since the event responses are sent from the server.
  38.         }, !1), s.addEventListener("comic/time/reload", function (e) {
  39.             var r = Math.round(Math.random() * 55);
  40.             n("reloading in", r + 5, "seconds"), setTimeout(function () {
  41.                 t("reloading"), setTimeout(function () {
  42.                     location.reload()
  43.                 }, 5e3)
  44.             }, r * 1e3)
  45.         }, !1)
  46.     } catch (u) {
  47.         t("js_error")
  48.     }
  49. }();
  50. [/code]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand