最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Javascript get every second slide with modulus? - Stack Overflow

matteradmin5PV0评论

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
Add a ment  | 

5 Answers 5

Reset to default 2

Mod 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.

Post a comment

comment list (0)

  1. No comments so far