Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript timers - clearing, setting new, cycling through divs
    primarykey
    data
    text
    <p>I have some JavaScript that cycles through n div tags, displaying only one at a time, and changes every 10s. This is working. But I also have next and previous buttons. They seem to be skipping one div when I click next or previous. </p> <p>Can someone help?</p> <p>Any suggestions on a better way to do this is also welcome. I'm new to jQuery so I know some improvements I'd like to do anyway. Thanks!!!</p> <pre><code>jQuery(document).ready(function () { var counter = 1; var delay = 3000; //10 seconds = 10000 var lastItem = jQuery('table[id^=featuredNo]').length - 1; var previous = 0; var timerID; //argument is increment //false is decrement function updateCounter(increment) { if (increment) { counter = (counter &gt;= lastItem) ? 0 : counter + 1; previous = (previous &gt;= lastItem) ? 0 : previous + 1; } else { counter = (counter &lt;= 0) ? lastItem : counter - 1; previous = (previous &lt;= 0) ? lastItem : previous - 1; } } function displayNext() { //alert("testt" + counter); //hide everything but current jQuery("table[id^=featuredNo]").hide(); jQuery("#featuredNo" + counter).show(); //incrememnt the counter, so next time we show the next one updateCounter(true); }; //set click handlers for previous jQuery('a[id^=featuredPrevious]').click(function () { clearInterval(timerID); updateCounter(false); displayNext(); timerID = setInterval(displayNext, delay); }); //set click handlers for next jQuery('a[id^=featuredNext]').click(function () { clearInterval(timerID); updateCounter(true); displayNext(); timerID = setInterval(displayNext, delay); }); //start interval timerID = setInterval(displayNext, delay); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload