var model1 = []; var model2 = []; var model3 = []; for (var i = 1; i < 101; i++) { model1.push("images/xl1/c (" + i + ").jpg") } for (var i = 1; i < 151; i++) { model2.push("images/xl2/c (" + i + ").jpg") } for (var i = 1; i < 151; i++) { model3.push("images/xl3/c (" + i + ").jpg") } function seq(arr, id, speed) { var th = this; this.id = $("." + id); this.elements = document.querySelector('.'+id) this.current = 0; this.xcurrrent = 0; this.nums = arr.length; this.loads(arr, function() { th.gotoAndplay(arr,speed); }) } seq.prototype.gotoAndplay = function(arr,speed) { var th = this; var modelplayer = ""; modelplayer = setInterval(function() { th.xcurrrent++; if (th.xcurrrent > th.nums -1) { //th.xcurrrent = 0; th.xcurrrent = th.nums; th.id.find("img").eq(th.nums - 1).css("opacity",1); clearInterval(modelplayer); setTimeout(function(){ th.xcurrrent = 0; th.gotoAndplay(arr,speed); },0) }else{ th.id.find("img").css("opacity",0); th.id.find("img").eq(th.xcurrrent ).css("opacity",1); } }, speed) } seq.prototype.loads = function(arr, callback) { var th = this; var img = new Image(); img.onload = function() { th.elements.appendChild(img); th.current++; if (th.current < th.nums) { th.loads(arr, callback); } else { callback(); } } img.src = arr[th.current]; } var model1 = new seq(model1, "model1",30); var model2 = new seq(model2, "model2",30); var model3 = new seq(model3, "model3",30);