$(document).ready(function () { var $imageColection, counterImgTotal, counterImg; function compareAndStop(){ if (counterImg === counterImgTotal - 1) { $('.l-wrapper').trigger('imagesLoaded'); setTimeout(function(){ $('html').removeClass('loader-lock'); $('.l-wrapper').addClass('animated fadeOut is-hide'); return false; }, 900); } counterImg++; } if ($('html').hasClass('loader-lock')) { $imageColection = $('img'); counterImgTotal = $imageColection.length; counterImg = 0; setTimeout(function () { $imageColection.each(function () { var img = new Image(); if (!$(this).attr('src')) { counterImg++; return true; } img.src = $(this).attr('src'); try { img.onload = function () { compareAndStop(); }; } catch(e){ compareAndStop(); } }); }, 250); } function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } });