I have a function that fires every time a slide is advanced in my slideshow, and increments a counter.
I want a simple way to call a function every X slide (5 in the example code). I realize I can do this easily using modulus but the syntax escapes me.
var numSlides = $('.item').length; // 20
var currSlide = 0;
var ad_interval = 5;
function updateSlide (){
currSlide++
// if we are at the ad interval, every 5 slides, call the next line
$("#adslot").reveal();
}
I have a function that fires every time a slide is advanced in my slideshow, and increments a counter.
I want a simple way to call a function every X slide (5 in the example code). I realize I can do this easily using modulus but the syntax escapes me.
var numSlides = $('.item').length; // 20
var currSlide = 0;
var ad_interval = 5;
function updateSlide (){
currSlide++
// if we are at the ad interval, every 5 slides, call the next line
$("#adslot").reveal();
}
Share
Improve this question
asked Mar 4, 2013 at 14:59
SteveSteve
14.9k39 gold badges139 silver badges245 bronze badges
5 Answers
Reset to default 2Mod returns the remainder... When it's 0, you've hit a count on the interval.
if((currSlide % ad_interval) === 0) {
$('#adslot').reveal();
}
if (currSlide % ad_interval === 0) {
// Note that this will also include the first (0th) slide.
}
Inside your updateSlide() method, you can include something like :
if(currSlide % ad_interval === 0)
{
// Run method code.
}
This will run a method every 5 slides. For more information on Java operators and the modulus operator, this site does a pretty good job of explaining them.
Re you looking for this?
if (currSlide%5 ==0) {
$("#adslot").reveal();
}
I'd suggest:
function updateSlide (){
// if we are at the ad interval, every 5 slides, call the next line
if ((currSlide + 1) % ad_interval == 0){
$("#adslot").reveal();
}
currslide++;
}
The reason I'm using (currSlide + 1) % ad_interval
is simply because your index is zero-based, and the fifth slide would be at index 4
, rather than 5
; the +1
simply ensures that the the modulus will be 0
at the fifth slide, rather than the sixth.
This will also prevent the first, zeroth, slide from showing the ad.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745663897a4639005.html
评论列表(0条)