当我尝试使用“this”调用一个函数时,出现了一个错误:“this.slideshow_action 不是一个函数”。你能告诉我我的错吗!?
var class_slider = {
current_slide: 0, //Starting Slide
total_slides: 4, //All Slides
slideshow_action: function(slide_number) {
$(".slide_text").hide();
$(".main_navigation_container").removeClass("selected");
$("#slide_" + slide_number).addClass("selected");
$("#slide_" + slide_number + "_text").fadeIn("slow");
},
slideshow: function() {
if (this.current_slide === this.total_slides)
this.current_slide = 0;//Make a new tour
else
this.current_slide++;
this.slideshow_action(this.current_slide);
},
reset_timer: function()
{
window.clearInterval(this.slideshow_timer);
this.slideshow_timer = window.setInterval(this.slideshow, 5000);
},
icon_action: function(element) {
this.reset_timer();
var arr = element.attr("id").split('_'); //Get Last Part of id(slide number)
this.current_slide = parseInt(arr[1]); //Get slide Number
this.slideshow_action(this.current_slide);
}
};
最佳答案
使用上下文。
setTimeout(function(context){
context._someMethod();
}, 5000, this);
}
var timer = setInterval(function(context){
context._someMethod();
}, 5000, this);
}
关于javascript - 使用 "this"调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20173485/