Javascript 我怎样才能使这个代码工作?
我有下面的代码,但我不能调用nextPromo,因为它不是一个函数。我怎样才能做到这一点?我正在尝试使用面向对象的样式设置旋转器。我是新手,所以我很困惑。我尝试了很多东西,但我不知道,我对此感到非常沮丧,请帮助我Javascript 我怎样才能使这个代码工作?,javascript,object,Javascript,Object,我有下面的代码,但我不能调用nextPromo,因为它不是一个函数。我怎样才能做到这一点?我正在尝试使用面向对象的样式设置旋转器。我是新手,所以我很困惑。我尝试了很多东西,但我不知道,我对此感到非常沮丧,请帮助我 function promoSlides(s){ this.index = 0; this.prevIndex = 0; this.currentVeh = ""; this.t; this.slides = s; this.len =
function promoSlides(s){
this.index = 0;
this.prevIndex = 0;
this.currentVeh = "";
this.t;
this.slides = s;
this.len = this.slides.length;
this.sortWeight = function(){
($('body').hasClass('en')) ? this.slides.sort(SortByWeight) : this.slides.sort(SortByWeightFr);
};
function SortByWeight(a,b) { return b.weight - a.weight; }
function SortByWeightFr(a,b) { return a.frWeight - b.frWeight; }
this.startTimer = function(){ this.t = setTimeout("this.nextPromo()", 3000); }
this.nextPromo = function(){
if(this.index > 0 || this.prevIndex > 0) $(this.slides[this.prevIndex].el).css("display","none");
$(this.slides[this.index].el).css("display","block");
this.prevIndex = this.index;
this.index = (this.index < this.len-1) ? this.index+1 : 0;
this.startTimer();
}
return true;
} ;
功能启动幻灯片{
该指数=0;
该指数=0;
此.currentVeh=“”;
这个,t,;
this.slides=s;
this.len=this.slides.length;
this.sortWeight=函数(){
($('body').hasClass('en')?this.slides.sort(SortByWeight):this.slides.sort(SortByWeightFr);
};
函数SortByWeight(a,b){返回b.weight-a.weight;}
函数SortByWeightFr(a,b){返回a.frWeight-b.frWeight;}
this.startTimer=function(){this.t=setTimeout(“this.nextPromo()”,3000);}
this.nextPromo=函数(){
如果(this.index>0 | | this.prevIndex>0)$(this.slides[this.prevIndex].el).css(“显示”、“无”);
$(this.slides[this.index].el).css(“显示”、“块”);
this.prevIndex=this.index;
this.index=(this.index
您正在为this.nextPromo
分配一个函数,因此调用this.nextPromo()代码>应该可以工作。实际上,nextPromo
是一个函数。你可以这样称呼它:
this.nextPromo();
或者,如果这不起作用,您可以像调用普通函数一样调用它:
nextPromo();
有关此类函数的示例,请参见
希望这有帮助。编辑:下面是一个我已经测试过的简化示例。这将使用setInterval定期推进幻灯片索引。“下一步”功能包含在匿名函数中
function promoSlides(s) {
this.startTimer = function () {
setInterval(
function () {
alert("called");
// if (this.index > 0 || this.prevIndex > 0) {
// $(this.slides[this.prevIndex].el).css("display", "none");
// }
// $(this.slides[this.index].el).css("display", "block");
// this.prevIndex = this.index;
// this.index = (this.index < this.len - 1) ? this.index + 1 : 0;
}, 3000
);
}
return true;
};
var p = new promoSlides(null);
p.startTimer();
功能启动幻灯片{
this.startTimer=函数(){
设定间隔(
函数(){
警报(“被呼叫”);
//如果(this.index>0 | | this.prevIndex>0){
//$(this.slides[this.prevIndex].el).css(“显示”、“无”);
// }
//$(this.slides[this.index].el).css(“显示”、“块”);
//this.prevIndex=this.index;
//this.index=(this.index
您使用了一个冗长的操作符,但没有真正的原因。settimeout中的引号是一个求值而不是函数调用。我删除了引号,它给出了一个错误,表示递归太多。这调用了函数,但我得到了太多的递归问题,我不明白它是如何发生的。它应该继续旋转。这是可行的,我之所以加入nextPromo是因为我最终会停下来暂停幻灯片,但当我想继续时,我希望它在继续之前等待3秒钟。我应该能够建立在这个基础上,谢谢,我去掉了引号,但是我得到了这个错误,说太多的递归,也许我把时间事件设置错了,我怎么能修复它呢it@user930795我认为把引语去掉会把事情搞砸。在你删除引号之前,错误是什么?编辑:没关系,我只是看到有人接了电话。