Javascript 在我的幻灯片放映中获得一张幻灯片,以便在间隔时间内停留更长时间
我试图完成的是一个幻灯片,其中一个幻灯片持续10秒,而幻灯片中的所有其他幻灯片持续4秒。在我下面的代码中,当前的.id==1条件是我的第一张幻灯片。加载DOM时,它会检查第一个幻灯片是什么,如果它是current.id==1,它会在10秒钟内进行所有转换。这不是我想要的。有什么建议可以让所有其他幻灯片每4秒间隔一次,并且每次主幻灯片翻转到该幻灯片时,主幻灯片仍能持续10秒 对不起,如果这有点混乱的话。谢谢你们的帮助Javascript 在我的幻灯片放映中获得一张幻灯片,以便在间隔时间内停留更长时间,javascript,jquery,Javascript,Jquery,我试图完成的是一个幻灯片,其中一个幻灯片持续10秒,而幻灯片中的所有其他幻灯片持续4秒。在我下面的代码中,当前的.id==1条件是我的第一张幻灯片。加载DOM时,它会检查第一个幻灯片是什么,如果它是current.id==1,它会在10秒钟内进行所有转换。这不是我想要的。有什么建议可以让所有其他幻灯片每4秒间隔一次,并且每次主幻灯片翻转到该幻灯片时,主幻灯片仍能持续10秒 对不起,如果这有点混乱的话。谢谢你们的帮助 /* Setting auto-advance every 10 seconds
/* Setting auto-advance every 10 seconds for main slide, 4 seconds for all others */
var auto;
if (current.id==1) { //this is the only slide I want lasting 10 seconds.
auto=setInterval(function(){
next();
},10*1000); //10 seconds for MAIN slide
} else { // all other slides should interval every 4 seconds.
auto=setInterval(function(){
next();
},4*1000);//4 seconds each other slide.
}
如果需要不同的间隔,则不能使用
setInterval()
,因为您只调用了一次,然后它以相同的间隔重复运行,直到停止为止。您可以在每张幻灯片上使用setTimeout()
,如下所示:
if (current.id == 1) { //this is the only slide I want lasting 10 seconds.
setTimeout(next, 10*1000); //10 seconds for MAIN slide
} else { // all other slides should interval every 4 seconds.
setTimeout(next, 4*1000);//4 seconds each other slide.
}
另外,如果您只想调用本例中已定义的函数,
setTimeout()
或setInterval()
则不需要匿名函数。您不能使用setInterval()
如果您想要一个不同的间隔,因为您只调用它一次,然后它以相同的间隔重复运行,直到您停止为止。您可以在每张幻灯片上使用setTimeout()
,如下所示:
if (current.id == 1) { //this is the only slide I want lasting 10 seconds.
setTimeout(next, 10*1000); //10 seconds for MAIN slide
} else { // all other slides should interval every 4 seconds.
setTimeout(next, 4*1000);//4 seconds each other slide.
}
另外,如果您只想调用本例中已定义的函数,
setTimeout()
或setInterval()
则不需要匿名函数。您应该使用setTimeout
而不是setInterval
var auto;
if (current.id==1) { //this is the only slide I want lasting 10 seconds.
auto=setTimeout(function(){
next();
},10*1000); //10 seconds for MAIN slide
} else { // all other slides should interval every 4 seconds.
auto=setTimeout(function(){
next();
},4*1000);//4 seconds each other slide.
}
您应该使用
setTimeout
而不是setInterval
var auto;
if (current.id==1) { //this is the only slide I want lasting 10 seconds.
auto=setTimeout(function(){
next();
},10*1000); //10 seconds for MAIN slide
} else { // all other slides should interval every 4 seconds.
auto=setTimeout(function(){
next();
},4*1000);//4 seconds each other slide.
}
我这样写我的JS。在我看来,这使它更具可读性,而且确实很有帮助,尽管通常我不包括这样的评论 您的
next()
函数应该调用自身
$(document).ready(function () {
var Page = {};
Page.slider = (function () {
return {
init: function () {
setTimeout(function () {
Page.slider.nextSlide();
}, 10*1000);
},
nextSlide: function () {
var duration = 4;
if (Page.slider.isShowingFirst()) {
duration = 10;
}
//code to handle transitioning slides
setTimeout(function () {
Page.slider.nextSlide();
}, duration*1000);
},
isShowingFirst: function () {
//return boolean on whether it's showing the first slide or not
}
}
})();
Page.slider.init();
});
我这样写我的JS。在我看来,这使它更具可读性,而且确实很有帮助,尽管通常我不包括这样的评论 您的
next()
函数应该调用自身
$(document).ready(function () {
var Page = {};
Page.slider = (function () {
return {
init: function () {
setTimeout(function () {
Page.slider.nextSlide();
}, 10*1000);
},
nextSlide: function () {
var duration = 4;
if (Page.slider.isShowingFirst()) {
duration = 10;
}
//code to handle transitioning slides
setTimeout(function () {
Page.slider.nextSlide();
}, duration*1000);
},
isShowingFirst: function () {
//return boolean on whether it's showing the first slide or not
}
}
})();
Page.slider.init();
});
感谢jfriend00的回复!感谢jfriend00的回复!请显示更多代码。我想看看
auto
如何与您的幻灯片交互,以便了解如何修改它以实现您正在尝试的功能。请显示更多代码。我想看看auto
是如何与您的幻灯片交互的,以便了解它需要如何修改才能完成您正在尝试的操作。