Echo89

Background Colour Randomiser

May 21st, 2016
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Randomise the colour of every element on the page every 5000ms
  3.  * Just for fun.
  4.  *
  5.  * @author Caelan Stewart
  6.  */
  7.  
  8. setInterval(function() {
  9.     var count = 0;
  10.    
  11.     function chaos(element) {
  12.         // Check that this element is standard element, and not a text node or some other kind
  13.         if(element.nodeType === 1) {
  14.             var childNodesLen = element.childNodes.length,
  15.                 iter;
  16.  
  17.             // Generate random colour
  18.             element.style.backgroundColor = '#' + ((1 << 24) * Math.random() | 0).toString(16);
  19.  
  20.             // Iterate through children and self-invoke this function for each child element.
  21.             for(iter = 0; iter < childNodesLen; ++iter) {
  22.                 chaos(element.childNodes[iter]);
  23.             }
  24.            
  25.             count++;
  26.         }
  27.     }
  28.  
  29.     var start = Date.now();
  30.    
  31.     chaos(document.body);
  32.    
  33.     console.log('Took: ' + (Date.now() - start) + 'ms');
  34.     console.log('Elements: ' + count);
  35. }, 5000);
RAW Paste Data