Advertisement
Mako-chan

Random

Aug 6th, 2015
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Author: <mvdw at airmail dot cc> */
  2.  
  3. "use strict";
  4.  
  5. (function() {
  6.     var JSONLoad = function(file, callback) {
  7.         var x = new XMLHttpRequest();
  8.         x.callback = callback;
  9.        
  10.         if(x.overrideMimeType)
  11.             x.overrideMimeType("application/json");
  12.        
  13.         x.open('GET', file, false);
  14.         x.onreadystatechange = function(){
  15.             if(this.readyState == 4) this.callback(this);
  16.         }
  17.         x.send(null);
  18.     };
  19.  
  20.     var Apply = function() {
  21.         var fx = function() {
  22.             return Math.floor(Math.random() * sources.length)
  23.         };
  24.         while (vx == old) {
  25.             vx = fx();
  26.             if (vx != old) {
  27.                 break;
  28.             }
  29.         }
  30.         sl.src = sources[vx];
  31.         old = vx;
  32.     };
  33.  
  34.     var Reload = function() {
  35.         var timeout = window.setTimeout(function(){
  36.             Apply();
  37.             Reload();
  38.         }, (delay || 1000));
  39.     };
  40.    
  41.     var sources;
  42.     var delay = 1000;
  43.     var old;
  44.     var sl = document.createElement('img');
  45.     var vx;
  46.  
  47.     // Apply the element to the body. You can have it appended to a custom
  48.     // element by changing 'document.body' to the requested DOM element.
  49.     document.body.appendChild(sl);
  50.  
  51.     // Load the .json file to the sources variable.
  52.     JSONLoad("images.json", function(o){
  53.         sources = JSON.parse(o.responseText.split("*/").slice(-1)[0].replace(/[\n ]/,""))[0].images;
  54.     });
  55.    
  56.     // Apply onload.
  57.     Apply();
  58.  
  59.     // If you only want a new one when you refresh your page you comment this.
  60.     Reload();
  61. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement