I am trying to create a slide show where I load the next slide before the current one is done. I have two prototypes for this. One for showing the current slide and then one for loading the next slide.
It worked fine until I put a setTimeout()
for a delay between the slides. The NextSlide()
function is calling itself after the setTimeout()
however then the this.nextSlide
is undefined even if it's loaded and added to this.nextSlide
successfully in LoadNextSlide()
.
$(document).ready(function(){
//Set width and height of canvas
var width = $(document).width();
var height = $(document).height();
$("#MKslide").css({"width":width+"px","height":height+"px"});
slideShow = new SlideShow(width, height, "2d");
slideShow.LoadNextSlide(null);
slideShow.NextSlide();
});
function SlideShow(canvasWidth, canvasHeight, dimension){
//Creating the slideshow
}
SlideShow.prototype.LoadNextSlide = function(currentSlideNumber){
var data = $.parseJSON($.ajax({
type: "POST",
url: "/cms/php/request.php",
dataType: "json",
data: {slideNumber : currentSlideNumber},
async: false
}).responseText);
this.nextSlide = data;
alert(this.nextSlide.id);
}
SlideShow.prototype.NextSlide = function(){
alert(this.nextSlide.id);
this.currentSlide = this.nextSlide; //Swap to next slide.
if(beginsWithHTTP(this.currentSlide.data)){
this.image.src = this.currentSlide.data;
}
else{
this.image.src = "http://" + this.currentSlide.data; //Set the current slide.
}
this.LoadNextSlide(this.currentSlide.id); //Preload next slide
window.setTimeout(this.NextSlide,this.currentSlide.duration * 1000);//wait x seconds before swapping
}