Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function () {
  2.     //Calls getOddNewImages every 10sec and updates every even/odd image based on isOdd
  3.     var isOdd = false;
  4.     setInterval(function () {
  5.         isOdd = getOddNewImages(isOdd);
  6.     }, 10000);
  7.     getOddNewImages(); //hva gjor denne?
  8. });
  9.  
  10. function getOddNewImages(isOdd) {
  11.     var documentHeight = $(document).height();
  12.     $.get('{event-url}')
  13.         .done(function (data) {
  14.             if (data < 1) return;
  15.  
  16.             //Randomize order of json
  17.             fisherYatesShuffle(data);
  18.  
  19.             //Get all image tags
  20.             var imgLst = $('img').map(function () { return this.src; }).get();
  21.             if (imgLst < 1) {
  22.                 //Create new grid of images
  23.                 imgLst = createImageGrid(data.length);
  24.             }
  25.  
  26.             var imagesData = JSON.parse(data);
  27.             var cImageNo = 0;
  28.             for (var i = 0; i < imgLst.length; i++) {
  29.                 if (!isOdd && i % 2 != 0) {continue;} //if even and current index is odd
  30.                 if (isOdd && i % 2 == 0) {continue;} //if odd and current index is even
  31.  
  32.                 //update image
  33.                 imgLst[0].Attr("src", imgLst[cImageNo]);
  34.                 cImageNo++;
  35.             };
  36.         });
  37.  
  38.     //I didnt decode this, probably important?
  39.     setTimeout(function () {
  40.         var imageWrapperHeight = $('#image-wrapper').height();
  41.         if (documentHeight < imageWrapperHeight) {
  42.             remove_last_image();
  43.  
  44.         }
  45.     }, 500);
  46.  
  47.  
  48.     return !isOdd;
  49. }
  50.  
  51. function createImageGrid(totImages) {
  52.     //Logikk for Ć„ lage riktig grid basert pĆ„
  53.     for (i = 0; i < imagesData.images.length; i++) {
  54.         var imageWrapperHeight = $('#image-wrapper').height();
  55.         if (documentHeight >= (imageWrapperHeight)) {
  56.             $('#appending_images').append("<li><img src=''></li>");
  57.         }
  58.     }
  59.  
  60.     return $('img').map(function () { return this.src; }).get();
  61. }
  62.  
  63. function fisherYatesShuffle(sourceArray) {
  64.     for (let n = 0; n < sourceArray.length - 1; n++) {
  65.         var k = n + Math.floor(Math.random() * (sourceArray.length - n));
  66.  
  67.         var temp = sourceArray[k];
  68.         sourceArray[k] = sourceArray[n];
  69.         sourceArray[n] = temp;
  70.     }
  71. }
  72. function remove_last_image() {
  73.     $('#appending_images li > img:last').css('display', 'none');
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement