我想创建一个幻灯片放映,我在当前完成之前加载下一张幻灯片。我有两个原型。一个用于显示当前幻灯片,然后用于加载下一张幻灯片。变量设置为undefined功能
它工作正常,直到我把setTimeout()
的幻灯片之间的延迟。在setTimeout()
之后,NextSlide()
函数自行调用,然而this.nextSlide
未定义,即使它被加载并成功添加到this.nextSlide
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
}